美文网首页
vscode插件快餐教程(8) - LSP文本同步

vscode插件快餐教程(8) - LSP文本同步

作者: Jtag特工 | 来源:发表于2019-06-04 20:03 被阅读0次

    vscode插件快餐教程(8) - LSP文本同步

    这一节开始我们介绍下通过LSP进行文本同步的方法。

    文件打开

    我们先从简单的做起,先监听文件的打开。
    我们看一下LSP协议中对此部分的支持,参数是DidChangeTextDocumentParams结构。


    LSP文本同步.png

    微软的SDK在LSP的基础上是做了封装的,我们看下封装后的接口:


    文本改变API.png

    当前,TextDocument提供了4个属性:

    • uri: 文件的URI
    • version: 文件的版本号
    • languageId: 编程语言
    • lineCount: 有多少行
      另外还有3个函数:
    • getText(): 获取文本
    • positionAt和offsetAt用于Position和offset的转换

    我们来看个例子:

    documents.onDidOpen(
        (event: TextDocumentChangeEvent) => {
            logger.debug(`on open:${event.document.uri}`);
            logger.debug(`file version:${event.document.version}`);
            logger.debug(`file content:${event.document.getText()}`);
            logger.debug(`language id:${event.document.languageId}`);
            logger.debug(`line count:${event.document.lineCount}`);
        }
    );
    

    我们来看一个运行的例子:

    [2019-06-04T18:11:31.999] [DEBUG] lsp_demo - on open:file:///Users/ziyingliuziying/test.vb
    [2019-06-04T18:11:31.999] [DEBUG] lsp_demo - file version:1
    [2019-06-04T18:11:31.999] [DEBUG] lsp_demo - file content:dim a as integer;
    TextView1
    Javascript
    Button3
    Test2
    
    [2019-06-04T18:11:31.999] [DEBUG] lsp_demo - language id:vb
    [2019-06-04T18:11:32.000] [DEBUG] lsp_demo - line count:6
    

    监听文件变化

    监听文件变化与监听打开文件基本上是一模一样的,代码如下:

    documents.onDidChangeContent(
        (e: TextDocumentChangeEvent) => {
            logger.debug('document change received.');
            logger.debug(`document version:${e.document.version}`);
            logger.debug(`text:${e.document.getText()}`);
            logger.debug(`language id:${e.document.languageId}`);
            logger.debug(`line count:${e.document.lineCount}`);
        }
    );
    
    [2019-06-04T18:30:34.329] [DEBUG] lsp_demo - document change received.
    [2019-06-04T18:30:34.329] [DEBUG] lsp_demo - document version:1
    [2019-06-04T18:30:34.329] [DEBUG] lsp_demo - text:dim a as integer;
    TextView1
    Javascript
    Button3
    Test2
    
    [2019-06-04T18:30:34.329] [DEBUG] lsp_demo - language id:vb
    [2019-06-04T18:30:34.329] [DEBUG] lsp_demo - line count:6
    
    [2019-06-04T18:30:39.457] [DEBUG] lsp_demo - document change received.
    [2019-06-04T18:30:39.457] [DEBUG] lsp_demo - document version:2
    [2019-06-04T18:30:39.457] [DEBUG] lsp_demo - text:
    
    [2019-06-04T18:30:39.457] [DEBUG] lsp_demo - language id:vb
    [2019-06-04T18:30:39.458] [DEBUG] lsp_demo - line count:2
    
    [2019-06-04T18:30:41.576] [DEBUG] lsp_demo - document change received.
    [2019-06-04T18:30:41.576] [DEBUG] lsp_demo - document version:3
    [2019-06-04T18:30:41.577] [DEBUG] lsp_demo - text:b
    [2019-06-04T18:30:41.577] [DEBUG] lsp_demo - language id:vb
    [2019-06-04T18:30:41.577] [DEBUG] lsp_demo - line count:1
    
    [2019-06-04T18:30:41.949] [DEBUG] lsp_demo - document change received.
    [2019-06-04T18:30:41.949] [DEBUG] lsp_demo - document version:4
    [2019-06-04T18:30:41.949] [DEBUG] lsp_demo - text:u
    [2019-06-04T18:30:41.949] [DEBUG] lsp_demo - language id:vb
    [2019-06-04T18:30:41.949] [DEBUG] lsp_demo - line count:1
    
    [2019-06-04T18:30:42.447] [DEBUG] lsp_demo - document change received.
    [2019-06-04T18:30:42.447] [DEBUG] lsp_demo - document version:5
    [2019-06-04T18:30:42.447] [DEBUG] lsp_demo - text:Button5
    [2019-06-04T18:30:42.447] [DEBUG] lsp_demo - language id:vb
    [2019-06-04T18:30:42.447] [DEBUG] lsp_demo - line count:1
    

    文本监听模式

    上面的监听方式是增量监听,使用TextDocumentSyncKind.Incremental模式,代码如下:

    connection.onInitialize((params: InitializeParams) => {
        return {
            capabilities: {
                textDocumentSync: {
                    openClose: true,
                    change: TextDocumentSyncKind.Incremental
                },
                completionProvider: {
                    resolveProvider: true
                }
            }
        };
    });
    

    增量模式是每次只传变化的部分。
    下面我们可以看看传全量模式与其的区别:

    connection.onInitialize((params: InitializeParams) => {
        return {
            capabilities: {
                textDocumentSync: {
                    openClose: true,
                    change: TextDocumentSyncKind.Full
                },
                completionProvider: {
                    resolveProvider: true
                }
            }
        };
    });
    

    全量模式下,每次变化后的全量都会通过消息传递过来,我们看个例子:

    [2019-06-04T19:52:12.305] [DEBUG] lsp_demo - document change received.
    [2019-06-04T19:52:12.305] [DEBUG] lsp_demo - document version:1
    [2019-06-04T19:52:12.305] [DEBUG] lsp_demo - text:dim a as integer;
    TextView1
    Javascript
    Button3
    Test2
    Button5
    
    [2019-06-04T19:52:12.305] [DEBUG] lsp_demo - language id:vb
    [2019-06-04T19:52:12.305] [DEBUG] lsp_demo - line count:7
    [2019-06-04T19:52:19.442] [DEBUG] lsp_demo - document change received.
    [2019-06-04T19:52:19.442] [DEBUG] lsp_demo - document version:2
    [2019-06-04T19:52:19.442] [DEBUG] lsp_demo - text:dim a as integer;
    TextView1
    Javascript
    Button3
    Test2
    Button5
    T
    [2019-06-04T19:52:19.443] [DEBUG] lsp_demo - language id:vb
    [2019-06-04T19:52:19.443] [DEBUG] lsp_demo - line count:7
    [2019-06-04T19:52:19.443] [DEBUG] lsp_demo - onCompletion
    [2019-06-04T19:52:19.787] [DEBUG] lsp_demo - document change received.
    [2019-06-04T19:52:19.787] [DEBUG] lsp_demo - document version:5
    [2019-06-04T19:52:19.787] [DEBUG] lsp_demo - text:dim a as integer;
    TextView1
    Javascript
    Button3
    Test2
    Button5
    Test
    [2019-06-04T19:52:19.787] [DEBUG] lsp_demo - language id:vb
    [2019-06-04T19:52:19.787] [DEBUG] lsp_demo - line count:7
    [2019-06-04T19:52:19.788] [DEBUG] lsp_demo - onCompletion
    [2019-06-04T19:52:21.877] [DEBUG] lsp_demo - document change received.
    [2019-06-04T19:52:21.877] [DEBUG] lsp_demo - document version:6
    [2019-06-04T19:52:21.877] [DEBUG] lsp_demo - text:dim a as integer;
    TextView1
    Javascript
    Button3
    Test2
    Button5
    Test
    
    [2019-06-04T19:52:21.877] [DEBUG] lsp_demo - language id:vb
    [2019-06-04T19:52:21.877] [DEBUG] lsp_demo - line count:8
    

    还可以选择TextDocumentSyncKind.None模式,这时候不同步文本信息。

    相关文章

      网友评论

          本文标题:vscode插件快餐教程(8) - LSP文本同步

          本文链接:https://www.haomeiwen.com/subject/hgrhxctx.html