mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-29 21:35:20 +00:00
Refactor vscode extension
This commit is contained in:
parent
e4fdfd1501
commit
69de7e2fd7
14 changed files with 518 additions and 415 deletions
58
editors/code/src/commands/apply_source_change.ts
Normal file
58
editors/code/src/commands/apply_source_change.ts
Normal file
|
@ -0,0 +1,58 @@
|
|||
import * as vscode from 'vscode';
|
||||
import * as lc from 'vscode-languageclient'
|
||||
|
||||
import { Server } from '../server';
|
||||
|
||||
interface FileSystemEdit {
|
||||
type: string;
|
||||
uri?: string;
|
||||
src?: string;
|
||||
dst?: string;
|
||||
}
|
||||
|
||||
export interface SourceChange {
|
||||
label: string,
|
||||
sourceFileEdits: lc.TextDocumentEdit[],
|
||||
fileSystemEdits: FileSystemEdit[],
|
||||
cursorPosition?: lc.TextDocumentPositionParams,
|
||||
}
|
||||
|
||||
export async function handle(change: SourceChange) {
|
||||
console.log(`applySOurceChange ${JSON.stringify(change)}`)
|
||||
let wsEdit = new vscode.WorkspaceEdit()
|
||||
for (let sourceEdit of change.sourceFileEdits) {
|
||||
let uri = Server.client.protocol2CodeConverter.asUri(sourceEdit.textDocument.uri)
|
||||
let edits = Server.client.protocol2CodeConverter.asTextEdits(sourceEdit.edits)
|
||||
wsEdit.set(uri, edits)
|
||||
}
|
||||
let created;
|
||||
let moved;
|
||||
for (let fsEdit of change.fileSystemEdits) {
|
||||
if (fsEdit.type == "createFile") {
|
||||
let uri = vscode.Uri.parse(fsEdit.uri!)
|
||||
wsEdit.createFile(uri)
|
||||
created = uri
|
||||
} else if (fsEdit.type == "moveFile") {
|
||||
let src = vscode.Uri.parse(fsEdit.src!)
|
||||
let dst = vscode.Uri.parse(fsEdit.dst!)
|
||||
wsEdit.renameFile(src, dst)
|
||||
moved = dst
|
||||
} else {
|
||||
console.error(`unknown op: ${JSON.stringify(fsEdit)}`)
|
||||
}
|
||||
}
|
||||
let toOpen = created || moved
|
||||
let toReveal = change.cursorPosition
|
||||
await vscode.workspace.applyEdit(wsEdit)
|
||||
if (toOpen) {
|
||||
let doc = await vscode.workspace.openTextDocument(toOpen)
|
||||
await vscode.window.showTextDocument(doc)
|
||||
} else if (toReveal) {
|
||||
let uri = Server.client.protocol2CodeConverter.asUri(toReveal.textDocument.uri)
|
||||
let position = Server.client.protocol2CodeConverter.asPosition(toReveal.position)
|
||||
let editor = vscode.window.activeTextEditor;
|
||||
if (!editor || editor.document.uri.toString() != uri.toString()) return
|
||||
if (!editor.selection.isEmpty) return
|
||||
editor!.selection = new vscode.Selection(position, position)
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue