vscode plug-in fast food tutorial (5) - Code Completion

vscode plug-in fast food tutorial (5) - Code Completion

The section we introduced the three-way handshake and the basic framework of the agreement lsp.
Let us first study the simplest functions of the protocol: send a notification to vscode.

LSP message window

In the LSP protocol, with the window there are three related agreements:

  • window/ShowMessage Notification
  • window/showMessage Request
  • window/logMessage Notification

We can send a message to the client using Connection.window.sendxxxMessage function.
Depending on the degree of information, divided into Information, Warning, and Error three levels.

For example, we can onInitialized, that is, the client and server-side three-way handshake after everything is ready, send a message to the client.

connection.onInitialized(() => {
    connection.window.showInformationMessage('Hello World! form server side');
});

The results show the following:
showMessage

Code completion

We warm up with a window notice, to test the link through unreasonable. Here we go straight to one of the themes we are most interested in: code completion.

Code completion is also very simple form, enter a TextDocumentPositionParams, the output is a CompletionItem array, this function is registered to connection.onCompletion in:

connection.onCompletion(
    (_textDocumentPosition: TextDocumentPositionParams): CompletionItem[] => {});

Completion of the code used in the main data structure as shown below:
Code completion

Wherein the kind is defined by an enumerated attribute:
CompletionItemKind

Please do not be scared, we look at a simple example, in fact, the basic realization of the method is very simple:

connection.onCompletion(
    (_textDocumentPosition: TextDocumentPositionParams): CompletionItem[] => {
        connection.console.log('[xulun]Position:' + _textDocumentPosition.textDocument);

        return [
            {
                label: 'TextView',
                kind: CompletionItemKind.Text,
                data: 1
            },
            {
                label: 'Button',
                kind: CompletionItemKind.Text,
                data: 2
            },
            {
                label: 'ListView',
                kind: CompletionItemKind.Text,
                data: 3
            }
        ];
    }
)

Details of the full complement of

In addition to supplemental information textDocument / completion addition, LSP supports completionItem / resolve request, inputs and outputs are CompletionItem, returned further information.
You can register support for completionItem / resolve request by connection.onCompletionResolve method:

connection.onCompletionResolve(
    (item: CompletionItem): CompletionItem => {
        if (item.data === 1) {
            item.detail = 'TextView';
            item.documentation = 'TextView documentation';
        } else if (item.data === 2) {
            item.detail = 'Button';
            item.documentation = 'JavaScript documentation';
        } else if (item.data === 3) {
            item.detail = 'ListView';
            item.documentation = 'ListView documentation';
        }
        return item;
    }
)

Operating results are as follows:
_1

Use parameter completion location information

Input parameters with location information will be sent to complete the application, we can control the information supplement based on this information.
We have an example to illustrate the next:

connection.onCompletion(
    (_textDocumentPosition: TextDocumentPositionParams): CompletionItem[] => {
        
        return [
            {
                label: 'TextView' + _textDocumentPosition.position.character,
                kind: CompletionItemKind.Text,
                data: 1
            },
            {
                label: 'Button' + _textDocumentPosition.position.line,
                kind: CompletionItemKind.Text,
                data: 2
            },
            {
                label: 'ListView',
                kind: CompletionItemKind.Text,
                data: 3
            }
        ];
    }
)

At this time, we not only complement a control name, which will increase the current line number or column number.
Here is the full complement operation Button, the current line number will increase to complement the information, we trigger complement the 934 line, so the full complement of information prompts into Button933:
_

Guess you like

Origin yq.aliyun.com/articles/704290