aboutsummaryrefslogtreecommitdiff
path: root/code
diff options
context:
space:
mode:
Diffstat (limited to 'code')
-rw-r--r--code/src/extension.ts91
1 files changed, 51 insertions, 40 deletions
diff --git a/code/src/extension.ts b/code/src/extension.ts
index 3b24b73b6..f2589ef2f 100644
--- a/code/src/extension.ts
+++ b/code/src/extension.ts
@@ -60,11 +60,8 @@ export function activate(context: vscode.ExtensionContext) {
60 textDocument: { uri: editor.document.uri.toString() }, 60 textDocument: { uri: editor.document.uri.toString() },
61 range: client.code2ProtocolConverter.asRange(editor.selection), 61 range: client.code2ProtocolConverter.asRange(editor.selection),
62 } 62 }
63 let response = await client.sendRequest<lc.TextEdit[]>("m/joinLines", request) 63 let change = await client.sendRequest<SourceChange>("m/joinLines", request)
64 let edits = client.protocol2CodeConverter.asTextEdits(response) 64 await applySourceChange(change)
65 let wsEdit = new vscode.WorkspaceEdit()
66 wsEdit.set(editor.document.uri, edits)
67 return vscode.workspace.applyEdit(wsEdit)
68 }) 65 })
69 registerCommand('libsyntax-rust.parentModule', async () => { 66 registerCommand('libsyntax-rust.parentModule', async () => {
70 let editor = vscode.window.activeTextEditor 67 let editor = vscode.window.activeTextEditor
@@ -113,28 +110,7 @@ export function activate(context: vscode.ExtensionContext) {
113 return await vscode.tasks.executeTask(task) 110 return await vscode.tasks.executeTask(task)
114 } 111 }
115 }) 112 })
116 registerCommand('libsyntax-rust.fsEdit', async (ops: FsOp[]) => { 113 registerCommand('libsyntax-rust.applySourceChange', applySourceChange)
117 let edit = new vscode.WorkspaceEdit()
118 let created;
119 let moved;
120 for (let op of ops) {
121 if (op.type == "createFile") {
122 let uri = vscode.Uri.parse(op.uri!)
123 edit.createFile(uri)
124 created = uri
125 } else if (op.type == "moveFile") {
126 let src = vscode.Uri.parse(op.src!)
127 let dst = vscode.Uri.parse(op.dst!)
128 edit.renameFile(src, dst)
129 moved = dst
130 } else {
131 console.error(`unknown op: ${JSON.stringify(op)}`)
132 }
133 }
134 await vscode.workspace.applyEdit(edit)
135 let doc = await vscode.workspace.openTextDocument((created || moved)!)
136 await vscode.window.showTextDocument(doc)
137 })
138 114
139 dispose(vscode.workspace.registerTextDocumentContentProvider( 115 dispose(vscode.workspace.registerTextDocumentContentProvider(
140 'libsyntax-rust', 116 'libsyntax-rust',
@@ -207,18 +183,6 @@ function startServer() {
207 ) 183 )
208 } 184 }
209 ) 185 )
210 client.onRequest(
211 new lc.RequestType<lc.Position, void, any, any>("m/moveCursor"),
212 (params: lc.Position, token: lc.CancellationToken) => {
213 let editor = vscode.window.activeTextEditor;
214 if (!editor) return
215 if (!editor.selection.isEmpty) return
216 let position = client.protocol2CodeConverter.asPosition(params)
217 afterLs(() => {
218 editor!.selection = new vscode.Selection(position, position)
219 })
220 }
221 )
222 }) 186 })
223 client.start(); 187 client.start();
224} 188}
@@ -383,9 +347,56 @@ function createTask(spec: Runnable): vscode.Task {
383 return t; 347 return t;
384} 348}
385 349
386interface FsOp { 350interface FileSystemEdit {
387 type: string; 351 type: string;
388 uri?: string; 352 uri?: string;
389 src?: string; 353 src?: string;
390 dst?: string; 354 dst?: string;
391} 355}
356
357interface SourceChange {
358 label: string,
359 sourceFileEdits: lc.TextDocumentEdit[],
360 fileSystemEdits: FileSystemEdit[],
361 cursorPosition?: lc.TextDocumentPositionParams,
362}
363
364async function applySourceChange(change: SourceChange) {
365 console.log(`applySOurceChange ${JSON.stringify(change)}`)
366 let wsEdit = new vscode.WorkspaceEdit()
367 for (let sourceEdit of change.sourceFileEdits) {
368 let uri = client.protocol2CodeConverter.asUri(sourceEdit.textDocument.uri)
369 let edits = client.protocol2CodeConverter.asTextEdits(sourceEdit.edits)
370 wsEdit.set(uri, edits)
371 }
372 let created;
373 let moved;
374 for (let fsEdit of change.fileSystemEdits) {
375 if (fsEdit.type == "createFile") {
376 let uri = vscode.Uri.parse(fsEdit.uri!)
377 wsEdit.createFile(uri)
378 created = uri
379 } else if (fsEdit.type == "moveFile") {
380 let src = vscode.Uri.parse(fsEdit.src!)
381 let dst = vscode.Uri.parse(fsEdit.dst!)
382 wsEdit.renameFile(src, dst)
383 moved = dst
384 } else {
385 console.error(`unknown op: ${JSON.stringify(fsEdit)}`)
386 }
387 }
388 let toOpen = created || moved
389 let toReveal = change.cursorPosition
390 await vscode.workspace.applyEdit(wsEdit)
391 if (toOpen) {
392 let doc = await vscode.workspace.openTextDocument(toOpen)
393 await vscode.window.showTextDocument(doc)
394 } else if (toReveal) {
395 let uri = client.protocol2CodeConverter.asUri(toReveal.textDocument.uri)
396 let position = client.protocol2CodeConverter.asPosition(toReveal.position)
397 let editor = vscode.window.activeTextEditor;
398 if (!editor || editor.document.uri != uri) return
399 if (!editor.selection.isEmpty) return
400 editor!.selection = new vscode.Selection(position, position)
401 }
402}