typescript - 如何通过语言服务器扩展在工作区中创建和编辑新文件

标签 typescript visual-studio-code vscode-extensions language-server-protocol

我如何获取语言服务器扩展来触发新文件的创建、编辑它并在附加的客户端工作区中显示它?

我有一个在 node.js 中使用 vscode-languageserver 编写的 LSP 扩展,它通过 onExecuteCommand 在服务器上 执行命令。我希望这个服务器端命令触发客户端创建一个新的文本文件,用一些文本填充它,以便它出现在客户端的打开文件的工作区列表中。

查看https://github.com/microsoft/vscode-languageserver-node/blob/master/client-tests/src/helpers.test.ts我相信我需要做的是创建一个 WorkspaceChange 对象,运行 createFile(),应用一些更改 (.insert),然后告诉客户端通过 connection.workspace.applyEdit() 应用编辑,但这不起作用 - 没有文件已创建且调试器中未抛出任何错误。

这是我在服务器的 onExecuteCommand 中的代码:

//add some text
const textToAdd: string = "test string";

//create new WorkspaceChange obj
let workspaceChange = new WorkspaceChange();

//uri of the file we want to create
let newuri = 'file:///c:/temp/create.txt';

//make a TextEditChange obj. Fails if you do not supply version
let change = workspaceChange.getTextEditChange({ uri: newuri, version: 10 });

// give it some text 
change.insert(Position.create(0, 1), textToAdd);

// add a createFile documentChange to the workspaceChange
workspaceChange.createFile(newuri);

// pass these edits to the client to apply:
let reply = connection.workspace.applyEdit(workspaceChange.edit);
console.log(reply); //always <Pending>

如果我提供了一个不存在的文件名,那么该过程将失败 - 工作区中不会创建或打开任何文件。

但是,如果我提供现有文件名,则会应用编辑并按预期在工作区中打开文件。

我认为这是因为我在 createFile 之前提供了编辑,但如果我在 createFile() 之前运行 getTextEditChange() 那么该过程将失败并出现错误“未为文档更改配置工作区编辑”

最佳答案

谢谢你的鼓励,是的,我过了一段时间才开始工作

这里实现了: https://github.com/proclaimforum/vscode-proclaimscript-language/blob/master/server/src/server.ts

第 476 行以后是值得注意的,其中:

  1. 构造一个 CreateFile 变量,保存要创建的文件的路径 (uri),并形成一个 CreateFiles 数组(第 476-482 行)
  2. 创建一个 WorkspaceEdit 变量,为 documentChanges 属性指定我们上面的 CreateFile 数组 (485)
  3. 我们将其传递给客户以通过 workspace.applyEdit (488) 进行申请。这实际上创建了文件。
  4. 要添加到新文档的文本首先形成一个 TextEdit 对象数组,由范围 (range:) 和内容 (newText:) (491-498) 组成
  5. 构造了一个 TextDocumentEdit 数组,其中包含我们的 TextEdit (500-503)
  6. 我们更新 workspaceEdit 变量属性以引用此 TextDocumentEdits 数组 (506)
  7. 最后要求我们连接的客户端应用编辑 (510)

我复制了下面的相关代码部分以供引用,但请查看 github 链接以获得完整的实现,包括设置客户端连接等、设置功能等。

//uri of new file
let currentPath :string = (thisdoc.uri).substr(0,thisdoc.uri.lastIndexOf('/'));

let newuri = currentPath+'/syntaxcheck.txt';

//construct a CreateFile variable
let createFile: CreateFile = { kind: 'create', uri: newuri };
//and make into array
let createFiles: CreateFile[] = [];
createFiles.push(createFile);

//make a new workspaceEdit variable, specifying a createFile document change
var workspaceEdit: WorkspaceEdit = { documentChanges: createFiles };

//pass to client to apply this edit
await connection.workspace.applyEdit(workspaceEdit);


//To insert the text (and pop up the window), create array of TextEdit
let textEdit: TextEdit[] = [];
//let document = documents.get(newuri);
let documentRange: Range = Range.create(0, 0, Number.MAX_VALUE, Number.MAX_VALUE);
//populate with the text, and where to insert (surely this is what workspaceChange.insert is for?)
let textEdits: TextEdit = { range: documentRange, newText: syntaxmessage };

textEdit.push(textEdits);

//make a new array of textDocumentEdits, containing our TextEdit (range and text)
let textDocumentEdit = TextDocumentEdit.create({ uri: newuri, version: 1 }, textEdit);
let textDocumentEdits: TextDocumentEdit[] = [];
textDocumentEdits.push(textDocumentEdit);

//set  our workspaceEdit variable to this new TextDocumentEdit
workspaceEdit = { documentChanges: textDocumentEdits };

//and finally apply this to our workspace.
// we can probably do this some more elegant way 
connection.workspace.applyEdit(workspaceEdit);


关于typescript - 如何通过语言服务器扩展在工作区中创建和编辑新文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57787621/

相关文章:

typescript - 在 TypeScript 中创建显式不安全私有(private)类成员访问函数

javascript - 订阅更改,但需要查看使用服务的位置的变量?

python - VSCode 自动完成功能不适用于从源代码安装的 OpenCV

node.js - vscode intellisense node.js 适用于 .js 文件 - 不适用于 .ts 文件

python - 在VS Code API中,如何获取Python环境路径?

visual-studio-code - 我可以在 VSCode 扩展中设置 editor.tokenColorCustomizations 吗?

regex - Visual Studio代码中使用什么正则表达式变体?

javascript - 如何比较 Jest 中的 ENOENT 错误?

visual-studio-code - 用于 Jest VSCode 扩展测试运行器

angular - 如何修复 "Error: Expected to be running in ' ProxyZone',但未找到。” 在 mocha 测试中?