From c4ca6e29c25df209c2733ef24b8b4eca70ee93a4 Mon Sep 17 00:00:00 2001 From: vsrs Date: Wed, 6 May 2020 16:01:35 +0300 Subject: Uniformed way to get Debug Lens target executable. --- editors/code/src/commands/runnables.ts | 59 +++++++++++++++++++--------------- 1 file changed, 33 insertions(+), 26 deletions(-) (limited to 'editors/code/src/commands') diff --git a/editors/code/src/commands/runnables.ts b/editors/code/src/commands/runnables.ts index d77e8188c..7bb8727e7 100644 --- a/editors/code/src/commands/runnables.ts +++ b/editors/code/src/commands/runnables.ts @@ -64,29 +64,19 @@ export function runSingle(ctx: Ctx): Cmd { }; } -function getLldbDebugConfig(config: ra.Runnable, sourceFileMap: Record): vscode.DebugConfiguration { +function getLldbDebugConfig(config: ra.Runnable, executable: string, sourceFileMap?: Record): vscode.DebugConfiguration { return { type: "lldb", request: "launch", name: config.label, - cargo: { - args: config.args, - }, + program: executable, args: config.extraArgs, cwd: config.cwd, sourceMap: sourceFileMap }; } -const debugOutput = vscode.window.createOutputChannel("Debug"); - -async function getCppvsDebugConfig(config: ra.Runnable, sourceFileMap: Record): Promise { - debugOutput.clear(); - - const cargo = new Cargo(config.cwd || '.', debugOutput); - const executable = await cargo.executableFromArgs(config.args); - - // if we are here, there were no compilation errors. +function getCppvsDebugConfig(config: ra.Runnable, executable: string, sourceFileMap?: Record): vscode.DebugConfiguration { return { type: (os.platform() === "win32") ? "cppvsdbg" : 'cppdbg', request: "launch", @@ -98,36 +88,53 @@ async function getCppvsDebugConfig(config: ra.Runnable, sourceFileMap: Record { + debugOutput.clear(); + + const cargo = new Cargo(config.cwd || '.', debugOutput); + const executable = await cargo.executableFromArgs(config.args); + + // if we are here, there were no compilation errors. + return executable; +} + +type DebugConfigProvider = (config: ra.Runnable, executable: string, sourceFileMap?: Record) => vscode.DebugConfiguration; + export function debugSingle(ctx: Ctx): Cmd { return async (config: ra.Runnable) => { const editor = ctx.activeRustEditor; if (!editor) return; - const lldbId = "vadimcn.vscode-lldb"; - const cpptoolsId = "ms-vscode.cpptools"; + const knownEngines: Record = { + "vadimcn.vscode-lldb": getLldbDebugConfig, + "ms-vscode.cpptools": getCppvsDebugConfig + }; + const debugOptions = ctx.config.debug; - const debugEngineId = ctx.config.debug.engine; let debugEngine = null; - if (debugEngineId === "auto") { - debugEngine = vscode.extensions.getExtension(lldbId); - if (!debugEngine) { - debugEngine = vscode.extensions.getExtension(cpptoolsId); + if (debugOptions.engine === "auto") { + for (var engineId in knownEngines) { + debugEngine = vscode.extensions.getExtension(engineId); + if (debugEngine) break; } } else { - debugEngine = vscode.extensions.getExtension(debugEngineId); + debugEngine = vscode.extensions.getExtension(debugOptions.engine); } if (!debugEngine) { - vscode.window.showErrorMessage(`Install [CodeLLDB](https://marketplace.visualstudio.com/items?itemName=${lldbId})` - + ` or [MS C++ tools](https://marketplace.visualstudio.com/items?itemName=${cpptoolsId}) extension for debugging.`); + vscode.window.showErrorMessage(`Install [CodeLLDB](https://marketplace.visualstudio.com/items?itemName=vadimcn.vscode-lldb)` + + ` or [MS C++ tools](https://marketplace.visualstudio.com/items?itemName=ms-vscode.cpptools) extension for debugging.`); return; } - const debugConfig = lldbId === debugEngine.id - ? getLldbDebugConfig(config, ctx.config.debug.sourceFileMap) - : await getCppvsDebugConfig(config, ctx.config.debug.sourceFileMap); + const executable = await getDebugExecutable(config); + const debugConfig = knownEngines[debugEngine.id](config, executable, debugOptions.sourceFileMap); + debugOutput.appendLine("Launching debug configuration:"); + debugOutput.appendLine(JSON.stringify(debugConfig, null, 2)); return vscode.debug.startDebugging(undefined, debugConfig); }; } -- cgit v1.2.3 From 5426e2927e317a5e78179a5bd74b9414c0651b86 Mon Sep 17 00:00:00 2001 From: vsrs Date: Thu, 7 May 2020 17:07:58 +0300 Subject: Add additional debug options --- editors/code/src/commands/runnables.ts | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) (limited to 'editors/code/src/commands') diff --git a/editors/code/src/commands/runnables.ts b/editors/code/src/commands/runnables.ts index 7bb8727e7..782a7ba89 100644 --- a/editors/code/src/commands/runnables.ts +++ b/editors/code/src/commands/runnables.ts @@ -91,8 +91,6 @@ function getCppvsDebugConfig(config: ra.Runnable, executable: string, sourceFile const debugOutput = vscode.window.createOutputChannel("Debug"); async function getDebugExecutable(config: ra.Runnable): Promise { - debugOutput.clear(); - const cargo = new Cargo(config.cwd || '.', debugOutput); const executable = await cargo.executableFromArgs(config.args); @@ -130,8 +128,16 @@ export function debugSingle(ctx: Ctx): Cmd { return; } + debugOutput.clear(); + if (ctx.config.debug.openUpDebugPane) { + debugOutput.show(true); + } + const executable = await getDebugExecutable(config); - const debugConfig = knownEngines[debugEngine.id](config, executable, debugOptions.sourceFileMap); + let debugConfig = knownEngines[debugEngine.id](config, executable, debugOptions.sourceFileMap); + for (var key in debugOptions.engineSettings) { + debugConfig[key] = (debugOptions.engineSettings as any)[key]; + } debugOutput.appendLine("Launching debug configuration:"); debugOutput.appendLine(JSON.stringify(debugConfig, null, 2)); -- cgit v1.2.3 From 435a17ecd8806f3ae81edf6277c17363b01f4334 Mon Sep 17 00:00:00 2001 From: vsrs Date: Thu, 7 May 2020 18:35:48 +0300 Subject: Add separate settings for each debug engine. --- editors/code/src/commands/runnables.ts | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'editors/code/src/commands') diff --git a/editors/code/src/commands/runnables.ts b/editors/code/src/commands/runnables.ts index 782a7ba89..e62de7d6e 100644 --- a/editors/code/src/commands/runnables.ts +++ b/editors/code/src/commands/runnables.ts @@ -134,9 +134,12 @@ export function debugSingle(ctx: Ctx): Cmd { } const executable = await getDebugExecutable(config); - let debugConfig = knownEngines[debugEngine.id](config, executable, debugOptions.sourceFileMap); - for (var key in debugOptions.engineSettings) { - debugConfig[key] = (debugOptions.engineSettings as any)[key]; + const debugConfig = knownEngines[debugEngine.id](config, executable, debugOptions.sourceFileMap); + if (debugConfig.type in debugOptions.engineSettings) { + const settingsMap = (debugOptions.engineSettings as any)[debugConfig.type]; + for (var key in settingsMap) { + debugConfig[key] = settingsMap[key]; + } } debugOutput.appendLine("Launching debug configuration:"); -- cgit v1.2.3 From 23f4859166ba16f02927b476aad2ae91e618b1ef Mon Sep 17 00:00:00 2001 From: vsrs Date: Thu, 7 May 2020 18:53:14 +0300 Subject: Add CodeLLDB Rust visualization --- editors/code/src/commands/runnables.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'editors/code/src/commands') diff --git a/editors/code/src/commands/runnables.ts b/editors/code/src/commands/runnables.ts index e62de7d6e..ae328d2a4 100644 --- a/editors/code/src/commands/runnables.ts +++ b/editors/code/src/commands/runnables.ts @@ -72,7 +72,8 @@ function getLldbDebugConfig(config: ra.Runnable, executable: string, sourceFileM program: executable, args: config.extraArgs, cwd: config.cwd, - sourceMap: sourceFileMap + sourceMap: sourceFileMap, + sourceLanguages: ["rust"] }; } -- cgit v1.2.3 From 31d5c8d4878911b21280b144b1aac19545509973 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Sun, 10 May 2020 21:05:09 +0800 Subject: Word fix --- editors/code/src/commands/syntax_tree.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'editors/code/src/commands') diff --git a/editors/code/src/commands/syntax_tree.ts b/editors/code/src/commands/syntax_tree.ts index cfcf47b2f..b80a18a47 100644 --- a/editors/code/src/commands/syntax_tree.ts +++ b/editors/code/src/commands/syntax_tree.ts @@ -225,7 +225,7 @@ class AstInspector implements vscode.HoverProvider, vscode.DefinitionProvider, D return doc.positionAt(targetOffset); } - // Shitty workaround for crlf line endings + // Dirty workaround for crlf line endings // We are still in this prehistoric era of carriage returns here... let line = 0; -- cgit v1.2.3 From 9a82ee0de25901447fc49a9337d7290c0e6f6532 Mon Sep 17 00:00:00 2001 From: veetaha Date: Sun, 10 May 2020 20:43:48 +0300 Subject: Fix "show syntax tree" command @matlkad please don't forget to keep it up-to-date! --- editors/code/src/commands/syntax_tree.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'editors/code/src/commands') diff --git a/editors/code/src/commands/syntax_tree.ts b/editors/code/src/commands/syntax_tree.ts index cfcf47b2f..b7616c21f 100644 --- a/editors/code/src/commands/syntax_tree.ts +++ b/editors/code/src/commands/syntax_tree.ts @@ -206,7 +206,7 @@ class AstInspector implements vscode.HoverProvider, vscode.DefinitionProvider, D } private parseRustTextRange(doc: vscode.TextDocument, astLine: string): undefined | vscode.Range { - const parsedRange = /\[(\d+); (\d+)\)/.exec(astLine); + const parsedRange = /(\d+)\.\.(\d+)/.exec(astLine); if (!parsedRange) return; const [begin, end] = parsedRange -- cgit v1.2.3 From 155f0601421620086a256c9e313568d5bd7391e0 Mon Sep 17 00:00:00 2001 From: vsrs Date: Mon, 11 May 2020 16:06:57 +0300 Subject: "rust-analyzer.debug" command --- editors/code/src/commands/runnables.ts | 149 ++++++++++----------------------- 1 file changed, 43 insertions(+), 106 deletions(-) (limited to 'editors/code/src/commands') diff --git a/editors/code/src/commands/runnables.ts b/editors/code/src/commands/runnables.ts index ae328d2a4..c1b872bce 100644 --- a/editors/code/src/commands/runnables.ts +++ b/editors/code/src/commands/runnables.ts @@ -1,43 +1,46 @@ import * as vscode from 'vscode'; import * as lc from 'vscode-languageclient'; import * as ra from '../rust-analyzer-api'; -import * as os from "os"; import { Ctx, Cmd } from '../ctx'; -import { Cargo } from '../cargo'; +import { startDebugSession } from '../debug'; + +async function selectRunnable(ctx: Ctx, prevRunnable: RunnableQuickPick | undefined): Promise { + const editor = ctx.activeRustEditor; + const client = ctx.client; + if (!editor || !client) return; + + const textDocument: lc.TextDocumentIdentifier = { + uri: editor.document.uri.toString(), + }; + + const runnables = await client.sendRequest(ra.runnables, { + textDocument, + position: client.code2ProtocolConverter.asPosition( + editor.selection.active, + ), + }); + const items: RunnableQuickPick[] = []; + if (prevRunnable) { + items.push(prevRunnable); + } + for (const r of runnables) { + if ( + prevRunnable && + JSON.stringify(prevRunnable.runnable) === JSON.stringify(r) + ) { + continue; + } + items.push(new RunnableQuickPick(r)); + } + return await vscode.window.showQuickPick(items); +} export function run(ctx: Ctx): Cmd { let prevRunnable: RunnableQuickPick | undefined; return async () => { - const editor = ctx.activeRustEditor; - const client = ctx.client; - if (!editor || !client) return; - - const textDocument: lc.TextDocumentIdentifier = { - uri: editor.document.uri.toString(), - }; - - const runnables = await client.sendRequest(ra.runnables, { - textDocument, - position: client.code2ProtocolConverter.asPosition( - editor.selection.active, - ), - }); - const items: RunnableQuickPick[] = []; - if (prevRunnable) { - items.push(prevRunnable); - } - for (const r of runnables) { - if ( - prevRunnable && - JSON.stringify(prevRunnable.runnable) === JSON.stringify(r) - ) { - continue; - } - items.push(new RunnableQuickPick(r)); - } - const item = await vscode.window.showQuickPick(items); + const item = await selectRunnable(ctx, prevRunnable); if (!item) return; item.detail = 'rerun'; @@ -64,88 +67,22 @@ export function runSingle(ctx: Ctx): Cmd { }; } -function getLldbDebugConfig(config: ra.Runnable, executable: string, sourceFileMap?: Record): vscode.DebugConfiguration { - return { - type: "lldb", - request: "launch", - name: config.label, - program: executable, - args: config.extraArgs, - cwd: config.cwd, - sourceMap: sourceFileMap, - sourceLanguages: ["rust"] - }; -} - -function getCppvsDebugConfig(config: ra.Runnable, executable: string, sourceFileMap?: Record): vscode.DebugConfiguration { - return { - type: (os.platform() === "win32") ? "cppvsdbg" : 'cppdbg', - request: "launch", - name: config.label, - program: executable, - args: config.extraArgs, - cwd: config.cwd, - sourceFileMap: sourceFileMap, - }; -} +export function debug(ctx: Ctx): Cmd { + let prevDebuggee: RunnableQuickPick | undefined; -const debugOutput = vscode.window.createOutputChannel("Debug"); - -async function getDebugExecutable(config: ra.Runnable): Promise { - const cargo = new Cargo(config.cwd || '.', debugOutput); - const executable = await cargo.executableFromArgs(config.args); + return async () => { + const item = await selectRunnable(ctx, prevDebuggee); + if (!item) return; - // if we are here, there were no compilation errors. - return executable; + item.detail = 'restart'; + prevDebuggee = item; + return await startDebugSession(ctx, item.runnable); + }; } -type DebugConfigProvider = (config: ra.Runnable, executable: string, sourceFileMap?: Record) => vscode.DebugConfiguration; - export function debugSingle(ctx: Ctx): Cmd { return async (config: ra.Runnable) => { - const editor = ctx.activeRustEditor; - if (!editor) return; - - const knownEngines: Record = { - "vadimcn.vscode-lldb": getLldbDebugConfig, - "ms-vscode.cpptools": getCppvsDebugConfig - }; - const debugOptions = ctx.config.debug; - - let debugEngine = null; - if (debugOptions.engine === "auto") { - for (var engineId in knownEngines) { - debugEngine = vscode.extensions.getExtension(engineId); - if (debugEngine) break; - } - } - else { - debugEngine = vscode.extensions.getExtension(debugOptions.engine); - } - - if (!debugEngine) { - vscode.window.showErrorMessage(`Install [CodeLLDB](https://marketplace.visualstudio.com/items?itemName=vadimcn.vscode-lldb)` - + ` or [MS C++ tools](https://marketplace.visualstudio.com/items?itemName=ms-vscode.cpptools) extension for debugging.`); - return; - } - - debugOutput.clear(); - if (ctx.config.debug.openUpDebugPane) { - debugOutput.show(true); - } - - const executable = await getDebugExecutable(config); - const debugConfig = knownEngines[debugEngine.id](config, executable, debugOptions.sourceFileMap); - if (debugConfig.type in debugOptions.engineSettings) { - const settingsMap = (debugOptions.engineSettings as any)[debugConfig.type]; - for (var key in settingsMap) { - debugConfig[key] = settingsMap[key]; - } - } - - debugOutput.appendLine("Launching debug configuration:"); - debugOutput.appendLine(JSON.stringify(debugConfig, null, 2)); - return vscode.debug.startDebugging(undefined, debugConfig); + await startDebugSession(ctx, config); }; } -- cgit v1.2.3 From fee0a9fa5a3dd84400108b33a1e8225dc364a9fa Mon Sep 17 00:00:00 2001 From: vsrs Date: Mon, 11 May 2020 18:00:15 +0300 Subject: "rust-analyzer.newDebugConfig" command --- editors/code/src/commands/runnables.ts | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) (limited to 'editors/code/src/commands') diff --git a/editors/code/src/commands/runnables.ts b/editors/code/src/commands/runnables.ts index c1b872bce..5e88eeae0 100644 --- a/editors/code/src/commands/runnables.ts +++ b/editors/code/src/commands/runnables.ts @@ -3,7 +3,7 @@ import * as lc from 'vscode-languageclient'; import * as ra from '../rust-analyzer-api'; import { Ctx, Cmd } from '../ctx'; -import { startDebugSession } from '../debug'; +import { startDebugSession, getDebugConfiguration } from '../debug'; async function selectRunnable(ctx: Ctx, prevRunnable: RunnableQuickPick | undefined): Promise { const editor = ctx.activeRustEditor; @@ -86,6 +86,34 @@ export function debugSingle(ctx: Ctx): Cmd { }; } +export function newDebugConfig(ctx: Ctx): Cmd { + return async () => { + const scope = ctx.activeRustEditor?.document.uri; + if (!scope) return; + + const item = await selectRunnable(ctx, undefined); + if (!item) return; + + const debugConfig = await getDebugConfiguration(ctx, item.runnable); + if (!debugConfig) return; + + const wsLaunchSection = vscode.workspace.getConfiguration("launch", scope); + const configurations = wsLaunchSection.get("configurations") || []; + + const index = configurations.findIndex(c => c.name === debugConfig.name); + if (index !== -1) { + const answer = await vscode.window.showErrorMessage(`Launch configuration '${debugConfig.name}' already exists!`, 'Cancel', 'Update'); + if (answer === "Cancel") return; + + configurations[index] = debugConfig; + } else { + configurations.push(debugConfig); + } + + await wsLaunchSection.update("configurations", configurations); + }; +} + class RunnableQuickPick implements vscode.QuickPickItem { public label: string; public description?: string | undefined; -- cgit v1.2.3 From 9f0a7eb97b4e047cebbe51ffd6f9e2092dd63e00 Mon Sep 17 00:00:00 2001 From: Pavan Kumar Sunkara Date: Fri, 24 Apr 2020 21:57:10 +0200 Subject: Make some stuff public so that they can be reused by other tools --- editors/code/src/commands/ssr.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'editors/code/src/commands') diff --git a/editors/code/src/commands/ssr.ts b/editors/code/src/commands/ssr.ts index 6fee051fd..4ef8cdf04 100644 --- a/editors/code/src/commands/ssr.ts +++ b/editors/code/src/commands/ssr.ts @@ -11,7 +11,7 @@ export function ssr(ctx: Ctx): Cmd { const options: vscode.InputBoxOptions = { value: "() ==>> ()", - prompt: "EnteR request, for example 'Foo($a:expr) ==> Foo::new($a)' ", + prompt: "Enter request, for example 'Foo($a:expr) ==> Foo::new($a)' ", validateInput: async (x: string) => { try { await client.sendRequest(ra.ssr, { query: x, parseOnly: true }); -- cgit v1.2.3 From be9b0609d55f9f49e4473b4ab2bc55583974fc2f Mon Sep 17 00:00:00 2001 From: vsrs Date: Thu, 14 May 2020 13:22:52 +0300 Subject: Runnable quick pick with buttons --- editors/code/src/commands/runnables.ts | 82 +++++++++++++++++++++++++--------- 1 file changed, 61 insertions(+), 21 deletions(-) (limited to 'editors/code/src/commands') diff --git a/editors/code/src/commands/runnables.ts b/editors/code/src/commands/runnables.ts index 5e88eeae0..b1d93fc34 100644 --- a/editors/code/src/commands/runnables.ts +++ b/editors/code/src/commands/runnables.ts @@ -5,7 +5,9 @@ import * as ra from '../rust-analyzer-api'; import { Ctx, Cmd } from '../ctx'; import { startDebugSession, getDebugConfiguration } from '../debug'; -async function selectRunnable(ctx: Ctx, prevRunnable: RunnableQuickPick | undefined): Promise { +const quickPickButtons = [{ iconPath: new vscode.ThemeIcon("save"), tooltip: "Save as a launch.json configurtation." }]; + +async function selectRunnable(ctx: Ctx, prevRunnable?: RunnableQuickPick, showButtons: boolean = true): Promise { const editor = ctx.activeRustEditor; const client = ctx.client; if (!editor || !client) return; @@ -33,7 +35,41 @@ async function selectRunnable(ctx: Ctx, prevRunnable: RunnableQuickPick | undefi } items.push(new RunnableQuickPick(r)); } - return await vscode.window.showQuickPick(items); + + return await new Promise((resolve) => { + const disposables: vscode.Disposable[] = []; + const close = (result?: RunnableQuickPick) => { + resolve(result); + disposables.forEach(d => d.dispose()); + }; + + const quickPick = vscode.window.createQuickPick(); + quickPick.items = items; + quickPick.title = "Select Runnable"; + if (showButtons) { + quickPick.buttons = quickPickButtons; + } + disposables.push( + quickPick.onDidHide(() => close()), + quickPick.onDidAccept(() => close(quickPick.selectedItems[0])), + quickPick.onDidTriggerButton((_button) => { + (async () => await makeDebugConfig(ctx, quickPick.activeItems[0]))(); + close(); + }), + quickPick.onDidChangeActive((active) => { + if (showButtons && active.length > 0) { + if (active[0].label.startsWith('cargo')) { + // save button makes no sense for `cargo test` or `cargo check` + quickPick.buttons = []; + } else if (quickPick.buttons.length === 0) { + quickPick.buttons = quickPickButtons; + } + } + }), + quickPick + ); + quickPick.show(); + }); } export function run(ctx: Ctx): Cmd { @@ -86,31 +122,35 @@ export function debugSingle(ctx: Ctx): Cmd { }; } -export function newDebugConfig(ctx: Ctx): Cmd { - return async () => { - const scope = ctx.activeRustEditor?.document.uri; - if (!scope) return; +async function makeDebugConfig(ctx: Ctx, item: RunnableQuickPick): Promise { + const scope = ctx.activeRustEditor?.document.uri; + if (!scope) return; - const item = await selectRunnable(ctx, undefined); - if (!item) return; + const debugConfig = await getDebugConfiguration(ctx, item.runnable); + if (!debugConfig) return; - const debugConfig = await getDebugConfiguration(ctx, item.runnable); - if (!debugConfig) return; + const wsLaunchSection = vscode.workspace.getConfiguration("launch", scope); + const configurations = wsLaunchSection.get("configurations") || []; - const wsLaunchSection = vscode.workspace.getConfiguration("launch", scope); - const configurations = wsLaunchSection.get("configurations") || []; + const index = configurations.findIndex(c => c.name === debugConfig.name); + if (index !== -1) { + const answer = await vscode.window.showErrorMessage(`Launch configuration '${debugConfig.name}' already exists!`, 'Cancel', 'Update'); + if (answer === "Cancel") return; - const index = configurations.findIndex(c => c.name === debugConfig.name); - if (index !== -1) { - const answer = await vscode.window.showErrorMessage(`Launch configuration '${debugConfig.name}' already exists!`, 'Cancel', 'Update'); - if (answer === "Cancel") return; + configurations[index] = debugConfig; + } else { + configurations.push(debugConfig); + } - configurations[index] = debugConfig; - } else { - configurations.push(debugConfig); - } + await wsLaunchSection.update("configurations", configurations); +} + +export function newDebugConfig(ctx: Ctx): Cmd { + return async () => { + const item = await selectRunnable(ctx, undefined, false); + if (!item) return; - await wsLaunchSection.update("configurations", configurations); + await makeDebugConfig(ctx, item); }; } -- cgit v1.2.3 From dec2f3fa657a2700f9db1962891e7be71c299543 Mon Sep 17 00:00:00 2001 From: vsrs Date: Sun, 17 May 2020 20:29:59 +0300 Subject: Runnable QuickPick with debuggees only --- editors/code/src/commands/runnables.ts | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) (limited to 'editors/code/src/commands') diff --git a/editors/code/src/commands/runnables.ts b/editors/code/src/commands/runnables.ts index b1d93fc34..a408021e7 100644 --- a/editors/code/src/commands/runnables.ts +++ b/editors/code/src/commands/runnables.ts @@ -7,7 +7,7 @@ import { startDebugSession, getDebugConfiguration } from '../debug'; const quickPickButtons = [{ iconPath: new vscode.ThemeIcon("save"), tooltip: "Save as a launch.json configurtation." }]; -async function selectRunnable(ctx: Ctx, prevRunnable?: RunnableQuickPick, showButtons: boolean = true): Promise { +async function selectRunnable(ctx: Ctx, prevRunnable?: RunnableQuickPick, debuggeeOnly = false, showButtons: boolean = true): Promise { const editor = ctx.activeRustEditor; const client = ctx.client; if (!editor || !client) return; @@ -33,9 +33,20 @@ async function selectRunnable(ctx: Ctx, prevRunnable?: RunnableQuickPick, showBu ) { continue; } + + if (debuggeeOnly && (r.label.startsWith('doctest') || r.label.startsWith('cargo'))) { + continue; + } items.push(new RunnableQuickPick(r)); } + if( items.length === 0 ) { + // it is the debug case, run always has at least 'cargo check ...' + // see crates\rust-analyzer\src\main_loop\handlers.rs, handle_runnables + vscode.window.showErrorMessage("There's no debug target!"); + return; + } + return await new Promise((resolve) => { const disposables: vscode.Disposable[] = []; const close = (result?: RunnableQuickPick) => { @@ -107,7 +118,7 @@ export function debug(ctx: Ctx): Cmd { let prevDebuggee: RunnableQuickPick | undefined; return async () => { - const item = await selectRunnable(ctx, prevDebuggee); + const item = await selectRunnable(ctx, prevDebuggee, true); if (!item) return; item.detail = 'restart'; @@ -147,7 +158,7 @@ async function makeDebugConfig(ctx: Ctx, item: RunnableQuickPick): Promise export function newDebugConfig(ctx: Ctx): Cmd { return async () => { - const item = await selectRunnable(ctx, undefined, false); + const item = await selectRunnable(ctx, undefined, true, false); if (!item) return; await makeDebugConfig(ctx, item); -- cgit v1.2.3 From 3d445256fe56f4a7ead64514fb57b79079973d84 Mon Sep 17 00:00:00 2001 From: vsrs Date: Sun, 17 May 2020 20:38:50 +0300 Subject: code formatting --- editors/code/src/commands/runnables.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'editors/code/src/commands') diff --git a/editors/code/src/commands/runnables.ts b/editors/code/src/commands/runnables.ts index a408021e7..0bd30fb07 100644 --- a/editors/code/src/commands/runnables.ts +++ b/editors/code/src/commands/runnables.ts @@ -40,7 +40,7 @@ async function selectRunnable(ctx: Ctx, prevRunnable?: RunnableQuickPick, debugg items.push(new RunnableQuickPick(r)); } - if( items.length === 0 ) { + if (items.length === 0) { // it is the debug case, run always has at least 'cargo check ...' // see crates\rust-analyzer\src\main_loop\handlers.rs, handle_runnables vscode.window.showErrorMessage("There's no debug target!"); -- cgit v1.2.3 From 3dd68c1ba3e72a0959bcdaa46e730a7ae4d9ed4c Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 18 May 2020 01:53:55 +0200 Subject: Implement client-side of SnippetTextEdit --- editors/code/src/commands/index.ts | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'editors/code/src/commands') diff --git a/editors/code/src/commands/index.ts b/editors/code/src/commands/index.ts index bdb7fc3b0..770d11bd3 100644 --- a/editors/code/src/commands/index.ts +++ b/editors/code/src/commands/index.ts @@ -4,6 +4,7 @@ import * as ra from '../rust-analyzer-api'; import { Ctx, Cmd } from '../ctx'; import * as sourceChange from '../source_change'; +import { assert } from '../util'; export * from './analyzer_status'; export * from './matching_brace'; @@ -51,3 +52,36 @@ export function selectAndApplySourceChange(ctx: Ctx): Cmd { } }; } + +export function applySnippetWorkspaceEdit(_ctx: Ctx): Cmd { + return async (edit: vscode.WorkspaceEdit) => { + assert(edit.entries().length === 1, `bad ws edit: ${JSON.stringify(edit)}`); + const [uri, edits] = edit.entries()[0]; + + const editor = vscode.window.visibleTextEditors.find((it) => it.document.uri.toString() === uri.toString()); + if (!editor) return; + + let editWithSnippet: vscode.TextEdit | undefined = undefined; + let lineDelta = 0; + await editor.edit((builder) => { + for (const indel of edits) { + if (indel.newText.indexOf('$0') !== -1) { + editWithSnippet = indel; + } else { + if (!editWithSnippet) { + lineDelta = (indel.newText.match(/\n/g) || []).length - (indel.range.end.line - indel.range.start.line); + } + builder.replace(indel.range, indel.newText); + } + } + }); + if (editWithSnippet) { + const snip = editWithSnippet as vscode.TextEdit; + const range = snip.range.with( + snip.range.start.with(snip.range.start.line + lineDelta), + snip.range.end.with(snip.range.end.line + lineDelta), + ); + await editor.insertSnippet(new vscode.SnippetString(snip.newText), range); + } + }; +} -- cgit v1.2.3 From 39ec581bf6e01cf2d7f33aacbe8879abf7ea3199 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 20 May 2020 00:49:08 +0200 Subject: Fix client-side snippets --- editors/code/src/commands/index.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'editors/code/src/commands') diff --git a/editors/code/src/commands/index.ts b/editors/code/src/commands/index.ts index 770d11bd3..0937b495c 100644 --- a/editors/code/src/commands/index.ts +++ b/editors/code/src/commands/index.ts @@ -65,7 +65,8 @@ export function applySnippetWorkspaceEdit(_ctx: Ctx): Cmd { let lineDelta = 0; await editor.edit((builder) => { for (const indel of edits) { - if (indel.newText.indexOf('$0') !== -1) { + const isSnippet = indel.newText.indexOf('$0') !== -1 || indel.newText.indexOf('${') !== -1; + if (isSnippet) { editWithSnippet = indel; } else { if (!editWithSnippet) { -- cgit v1.2.3 From 4b495da368162a5b373d078be4ff51e55bffdf69 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 21 May 2020 14:26:44 +0200 Subject: Transition OnEnter to WorkspaceSnippetEdit This also changes our handiling of snippet edits on the client side. `editor.insertSnippet` unfortunately forces indentation, which we really don't want to have to deal with. So, let's just implement our manual hacky way of dealing with a simple subset of snippets we actually use in rust-analyzer --- editors/code/src/commands/index.ts | 75 ++++++++++++++++++++++------------- editors/code/src/commands/on_enter.ts | 5 ++- 2 files changed, 51 insertions(+), 29 deletions(-) (limited to 'editors/code/src/commands') diff --git a/editors/code/src/commands/index.ts b/editors/code/src/commands/index.ts index 0937b495c..e5ed77e32 100644 --- a/editors/code/src/commands/index.ts +++ b/editors/code/src/commands/index.ts @@ -53,36 +53,57 @@ export function selectAndApplySourceChange(ctx: Ctx): Cmd { }; } -export function applySnippetWorkspaceEdit(_ctx: Ctx): Cmd { +export function applySnippetWorkspaceEditCommand(_ctx: Ctx): Cmd { return async (edit: vscode.WorkspaceEdit) => { - assert(edit.entries().length === 1, `bad ws edit: ${JSON.stringify(edit)}`); - const [uri, edits] = edit.entries()[0]; + await applySnippetWorkspaceEdit(edit); + }; +} + +export async function applySnippetWorkspaceEdit(edit: vscode.WorkspaceEdit) { + assert(edit.entries().length === 1, `bad ws edit: ${JSON.stringify(edit)}`); + const [uri, edits] = edit.entries()[0]; - const editor = vscode.window.visibleTextEditors.find((it) => it.document.uri.toString() === uri.toString()); - if (!editor) return; + const editor = vscode.window.visibleTextEditors.find((it) => it.document.uri.toString() === uri.toString()); + if (!editor) return; - let editWithSnippet: vscode.TextEdit | undefined = undefined; - let lineDelta = 0; - await editor.edit((builder) => { - for (const indel of edits) { - const isSnippet = indel.newText.indexOf('$0') !== -1 || indel.newText.indexOf('${') !== -1; - if (isSnippet) { - editWithSnippet = indel; - } else { - if (!editWithSnippet) { - lineDelta = (indel.newText.match(/\n/g) || []).length - (indel.range.end.line - indel.range.start.line); - } - builder.replace(indel.range, indel.newText); - } + let selection: vscode.Selection | undefined = undefined; + let lineDelta = 0; + await editor.edit((builder) => { + for (const indel of edits) { + const parsed = parseSnippet(indel.newText); + if (parsed) { + const [newText, [placeholderStart, placeholderLength]] = parsed; + const prefix = newText.substr(0, placeholderStart); + const lastNewline = prefix.lastIndexOf('\n'); + + const startLine = indel.range.start.line + lineDelta + countLines(prefix); + const startColumn = lastNewline === -1 ? + indel.range.start.character + placeholderStart + : prefix.length - lastNewline - 1; + const endColumn = startColumn + placeholderLength; + selection = new vscode.Selection( + new vscode.Position(startLine, startColumn), + new vscode.Position(startLine, endColumn), + ); + builder.replace(indel.range, newText); + } else { + lineDelta = countLines(indel.newText) - (indel.range.end.line - indel.range.start.line); + builder.replace(indel.range, indel.newText); } - }); - if (editWithSnippet) { - const snip = editWithSnippet as vscode.TextEdit; - const range = snip.range.with( - snip.range.start.with(snip.range.start.line + lineDelta), - snip.range.end.with(snip.range.end.line + lineDelta), - ); - await editor.insertSnippet(new vscode.SnippetString(snip.newText), range); } - }; + }); + if (selection) editor.selection = selection; +} + +function parseSnippet(snip: string): [string, [number, number]] | undefined { + const m = snip.match(/\$(0|\{0:([^}]*)\})/); + if (!m) return undefined; + const placeholder = m[2] ?? ""; + const range: [number, number] = [m.index!!, placeholder.length]; + const insert = snip.replace(m[0], placeholder); + return [insert, range]; +} + +function countLines(text: string): number { + return (text.match(/\n/g) || []).length; } diff --git a/editors/code/src/commands/on_enter.ts b/editors/code/src/commands/on_enter.ts index 285849db7..a7871c31e 100644 --- a/editors/code/src/commands/on_enter.ts +++ b/editors/code/src/commands/on_enter.ts @@ -1,8 +1,8 @@ import * as vscode from 'vscode'; import * as ra from '../rust-analyzer-api'; -import { applySourceChange } from '../source_change'; import { Cmd, Ctx } from '../ctx'; +import { applySnippetWorkspaceEdit } from '.'; async function handleKeypress(ctx: Ctx) { const editor = ctx.activeRustEditor; @@ -21,7 +21,8 @@ async function handleKeypress(ctx: Ctx) { }); if (!change) return false; - await applySourceChange(ctx, change); + const workspaceEdit = client.protocol2CodeConverter.asWorkspaceEdit(change); + await applySnippetWorkspaceEdit(workspaceEdit); return true; } -- cgit v1.2.3 From 5b5ebec440841ee98a0aa70b71a135d94f5ca077 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 21 May 2020 19:50:23 +0200 Subject: Formalize JoinLines protocol extension --- editors/code/src/commands/join_lines.ts | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) (limited to 'editors/code/src/commands') diff --git a/editors/code/src/commands/join_lines.ts b/editors/code/src/commands/join_lines.ts index de0614653..0bf1ee6e6 100644 --- a/editors/code/src/commands/join_lines.ts +++ b/editors/code/src/commands/join_lines.ts @@ -1,7 +1,7 @@ import * as ra from '../rust-analyzer-api'; +import * as lc from 'vscode-languageclient'; import { Ctx, Cmd } from '../ctx'; -import { applySourceChange } from '../source_change'; export function joinLines(ctx: Ctx): Cmd { return async () => { @@ -9,10 +9,14 @@ export function joinLines(ctx: Ctx): Cmd { const client = ctx.client; if (!editor || !client) return; - const change = await client.sendRequest(ra.joinLines, { - range: client.code2ProtocolConverter.asRange(editor.selection), + const items: lc.TextEdit[] = await client.sendRequest(ra.joinLines, { + ranges: editor.selections.map((it) => client.code2ProtocolConverter.asRange(it)), textDocument: { uri: editor.document.uri.toString() }, }); - await applySourceChange(ctx, change); + editor.edit((builder) => { + client.protocol2CodeConverter.asTextEdits(items).forEach((edit) => { + builder.replace(edit.range, edit.newText); + }); + }); }; } -- cgit v1.2.3