mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-14 14:13:58 +00:00
Refactor applySourceChange
This commit is contained in:
parent
83d2527880
commit
5aebf1081d
7 changed files with 68 additions and 89 deletions
|
@ -40,11 +40,10 @@ export function analyzerStatus(ctx: Ctx): Cmd {
|
||||||
class TextDocumentContentProvider
|
class TextDocumentContentProvider
|
||||||
implements vscode.TextDocumentContentProvider {
|
implements vscode.TextDocumentContentProvider {
|
||||||
|
|
||||||
|
ctx: Ctx
|
||||||
uri = vscode.Uri.parse('rust-analyzer-status://status');
|
uri = vscode.Uri.parse('rust-analyzer-status://status');
|
||||||
eventEmitter = new vscode.EventEmitter<vscode.Uri>();
|
eventEmitter = new vscode.EventEmitter<vscode.Uri>();
|
||||||
|
|
||||||
ctx: Ctx
|
|
||||||
|
|
||||||
constructor(ctx: Ctx) {
|
constructor(ctx: Ctx) {
|
||||||
this.ctx = ctx
|
this.ctx = ctx
|
||||||
}
|
}
|
||||||
|
@ -53,9 +52,8 @@ class TextDocumentContentProvider
|
||||||
_uri: vscode.Uri,
|
_uri: vscode.Uri,
|
||||||
): vscode.ProviderResult<string> {
|
): vscode.ProviderResult<string> {
|
||||||
const editor = vscode.window.activeTextEditor;
|
const editor = vscode.window.activeTextEditor;
|
||||||
if (editor == null) {
|
if (editor == null) return '';
|
||||||
return '';
|
|
||||||
}
|
|
||||||
return this.ctx.client.sendRequest<string>(
|
return this.ctx.client.sendRequest<string>(
|
||||||
'rust-analyzer/analyzerStatus',
|
'rust-analyzer/analyzerStatus',
|
||||||
null,
|
null,
|
||||||
|
|
|
@ -3,10 +3,9 @@ import { Ctx, Cmd } from '../ctx'
|
||||||
import { analyzerStatus } from './analyzer_status';
|
import { analyzerStatus } from './analyzer_status';
|
||||||
import { matchingBrace } from './matching_brace';
|
import { matchingBrace } from './matching_brace';
|
||||||
import { joinLines } from './join_lines';
|
import { joinLines } from './join_lines';
|
||||||
import * as applySourceChange from './apply_source_change';
|
import { onEnter } from './on_enter';
|
||||||
import * as expandMacro from './expand_macro';
|
import * as expandMacro from './expand_macro';
|
||||||
import * as inlayHints from './inlay_hints';
|
import * as inlayHints from './inlay_hints';
|
||||||
import * as onEnter from './on_enter';
|
|
||||||
import * as parentModule from './parent_module';
|
import * as parentModule from './parent_module';
|
||||||
import * as runnables from './runnables';
|
import * as runnables from './runnables';
|
||||||
import * as syntaxTree from './syntaxTree';
|
import * as syntaxTree from './syntaxTree';
|
||||||
|
@ -17,7 +16,6 @@ function collectGarbage(ctx: Ctx): Cmd {
|
||||||
|
|
||||||
export {
|
export {
|
||||||
analyzerStatus,
|
analyzerStatus,
|
||||||
applySourceChange,
|
|
||||||
expandMacro,
|
expandMacro,
|
||||||
joinLines,
|
joinLines,
|
||||||
matchingBrace,
|
matchingBrace,
|
||||||
|
|
|
@ -1,16 +1,14 @@
|
||||||
import { Range, TextDocumentIdentifier } from 'vscode-languageclient';
|
import { Range, TextDocumentIdentifier } from 'vscode-languageclient';
|
||||||
import { Ctx, Cmd } from '../ctx';
|
import { Ctx, Cmd } from '../ctx';
|
||||||
import {
|
import {
|
||||||
handle as applySourceChange,
|
applySourceChange, SourceChange
|
||||||
SourceChange,
|
} from '../source_change';
|
||||||
} from './apply_source_change';
|
|
||||||
|
|
||||||
export function joinLines(ctx: Ctx): Cmd {
|
export function joinLines(ctx: Ctx): Cmd {
|
||||||
return async () => {
|
return async () => {
|
||||||
const editor = ctx.activeRustEditor;
|
const editor = ctx.activeRustEditor;
|
||||||
if (!editor) {
|
if (!editor) return;
|
||||||
return;
|
|
||||||
}
|
|
||||||
const request: JoinLinesParams = {
|
const request: JoinLinesParams = {
|
||||||
range: ctx.client.code2ProtocolConverter.asRange(editor.selection),
|
range: ctx.client.code2ProtocolConverter.asRange(editor.selection),
|
||||||
textDocument: { uri: editor.document.uri.toString() },
|
textDocument: { uri: editor.document.uri.toString() },
|
||||||
|
@ -19,7 +17,7 @@ export function joinLines(ctx: Ctx): Cmd {
|
||||||
'rust-analyzer/joinLines',
|
'rust-analyzer/joinLines',
|
||||||
request,
|
request,
|
||||||
);
|
);
|
||||||
await applySourceChange(change);
|
await applySourceChange(ctx, change);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,33 +1,28 @@
|
||||||
import * as vscode from 'vscode';
|
|
||||||
import * as lc from 'vscode-languageclient';
|
import * as lc from 'vscode-languageclient';
|
||||||
import { Server } from '../server';
|
|
||||||
import {
|
import {
|
||||||
handle as applySourceChange,
|
applySourceChange,
|
||||||
SourceChange,
|
SourceChange,
|
||||||
} from './apply_source_change';
|
} from '../source_change';
|
||||||
|
import { Cmd, Ctx } from '../ctx';
|
||||||
|
|
||||||
export async function handle(event: { text: string }): Promise<boolean> {
|
export function onEnter(ctx: Ctx): Cmd {
|
||||||
const editor = vscode.window.activeTextEditor;
|
return async (event: { text: string }) => {
|
||||||
if (
|
const editor = ctx.activeRustEditor;
|
||||||
editor == null ||
|
if (!editor || event.text !== '\n') return false;
|
||||||
editor.document.languageId !== 'rust' ||
|
|
||||||
event.text !== '\n'
|
const request: lc.TextDocumentPositionParams = {
|
||||||
) {
|
textDocument: { uri: editor.document.uri.toString() },
|
||||||
return false;
|
position: ctx.client.code2ProtocolConverter.asPosition(
|
||||||
|
editor.selection.active,
|
||||||
|
),
|
||||||
|
};
|
||||||
|
const change = await ctx.client.sendRequest<undefined | SourceChange>(
|
||||||
|
'rust-analyzer/onEnter',
|
||||||
|
request,
|
||||||
|
);
|
||||||
|
if (!change) return false;
|
||||||
|
|
||||||
|
await applySourceChange(ctx, change);
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
const request: lc.TextDocumentPositionParams = {
|
|
||||||
textDocument: { uri: editor.document.uri.toString() },
|
|
||||||
position: Server.client.code2ProtocolConverter.asPosition(
|
|
||||||
editor.selection.active,
|
|
||||||
),
|
|
||||||
};
|
|
||||||
const change = await Server.client.sendRequest<undefined | SourceChange>(
|
|
||||||
'rust-analyzer/onEnter',
|
|
||||||
request,
|
|
||||||
);
|
|
||||||
if (!change) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
await applySourceChange(change);
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,6 +27,28 @@ export class Ctx {
|
||||||
this.pushCleanup(d);
|
this.pushCleanup(d);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
overrideCommand(name: string, factory: (ctx: Ctx) => Cmd) {
|
||||||
|
const defaultCmd = `default:${name}`;
|
||||||
|
const override = factory(this);
|
||||||
|
const original = (...args: any[]) =>
|
||||||
|
vscode.commands.executeCommand(defaultCmd, ...args);
|
||||||
|
try {
|
||||||
|
const d = vscode.commands.registerCommand(
|
||||||
|
name,
|
||||||
|
async (...args: any[]) => {
|
||||||
|
if (!(await override(...args))) {
|
||||||
|
return await original(...args);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
);
|
||||||
|
this.pushCleanup(d);
|
||||||
|
} catch (_) {
|
||||||
|
vscode.window.showWarningMessage(
|
||||||
|
'Enhanced typing feature is disabled because of incompatibility with VIM extension, consider turning off rust-analyzer.enableEnhancedTyping: https://github.com/rust-analyzer/rust-analyzer/blob/master/docs/user/README.md#settings',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pushCleanup(d: { dispose(): any }) {
|
pushCleanup(d: { dispose(): any }) {
|
||||||
this.extCtx.subscriptions.push(d);
|
this.extCtx.subscriptions.push(d);
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,44 +27,12 @@ export async function activate(context: vscode.ExtensionContext) {
|
||||||
function registerCommand(name: string, f: any) {
|
function registerCommand(name: string, f: any) {
|
||||||
disposeOnDeactivation(vscode.commands.registerCommand(name, f));
|
disposeOnDeactivation(vscode.commands.registerCommand(name, f));
|
||||||
}
|
}
|
||||||
function overrideCommand(
|
|
||||||
name: string,
|
|
||||||
f: (...args: any[]) => Promise<boolean>,
|
|
||||||
) {
|
|
||||||
const defaultCmd = `default:${name}`;
|
|
||||||
const original = (...args: any[]) =>
|
|
||||||
vscode.commands.executeCommand(defaultCmd, ...args);
|
|
||||||
|
|
||||||
try {
|
|
||||||
registerCommand(name, async (...args: any[]) => {
|
|
||||||
const editor = vscode.window.activeTextEditor;
|
|
||||||
if (
|
|
||||||
!editor ||
|
|
||||||
!editor.document ||
|
|
||||||
editor.document.languageId !== 'rust'
|
|
||||||
) {
|
|
||||||
return await original(...args);
|
|
||||||
}
|
|
||||||
if (!(await f(...args))) {
|
|
||||||
return await original(...args);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
} catch (_) {
|
|
||||||
vscode.window.showWarningMessage(
|
|
||||||
'Enhanced typing feature is disabled because of incompatibility with VIM extension, consider turning off rust-analyzer.enableEnhancedTyping: https://github.com/rust-analyzer/rust-analyzer/blob/master/docs/user/README.md#settings',
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Commands are requests from vscode to the language server
|
// Commands are requests from vscode to the language server
|
||||||
registerCommand('rust-analyzer.parentModule', commands.parentModule.handle);
|
registerCommand('rust-analyzer.parentModule', commands.parentModule.handle);
|
||||||
registerCommand('rust-analyzer.run', commands.runnables.handle);
|
registerCommand('rust-analyzer.run', commands.runnables.handle);
|
||||||
// Unlike the above this does not send requests to the language server
|
// Unlike the above this does not send requests to the language server
|
||||||
registerCommand('rust-analyzer.runSingle', commands.runnables.handleSingle);
|
registerCommand('rust-analyzer.runSingle', commands.runnables.handleSingle);
|
||||||
registerCommand(
|
|
||||||
'rust-analyzer.applySourceChange',
|
|
||||||
commands.applySourceChange.handle,
|
|
||||||
);
|
|
||||||
registerCommand(
|
registerCommand(
|
||||||
'rust-analyzer.showReferences',
|
'rust-analyzer.showReferences',
|
||||||
(uri: string, position: lc.Position, locations: lc.Location[]) => {
|
(uri: string, position: lc.Position, locations: lc.Location[]) => {
|
||||||
|
@ -78,7 +46,7 @@ export async function activate(context: vscode.ExtensionContext) {
|
||||||
);
|
);
|
||||||
|
|
||||||
if (Server.config.enableEnhancedTyping) {
|
if (Server.config.enableEnhancedTyping) {
|
||||||
overrideCommand('type', commands.onEnter.handle);
|
ctx.overrideCommand('type', commands.onEnter);
|
||||||
}
|
}
|
||||||
|
|
||||||
const watchStatus = new StatusDisplay(
|
const watchStatus = new StatusDisplay(
|
||||||
|
@ -91,15 +59,15 @@ export async function activate(context: vscode.ExtensionContext) {
|
||||||
string,
|
string,
|
||||||
lc.GenericNotificationHandler,
|
lc.GenericNotificationHandler,
|
||||||
]> = [
|
]> = [
|
||||||
[
|
[
|
||||||
'rust-analyzer/publishDecorations',
|
'rust-analyzer/publishDecorations',
|
||||||
notifications.publishDecorations.handle,
|
notifications.publishDecorations.handle,
|
||||||
],
|
],
|
||||||
[
|
[
|
||||||
'$/progress',
|
'$/progress',
|
||||||
params => watchStatus.handleProgressNotification(params),
|
params => watchStatus.handleProgressNotification(params),
|
||||||
],
|
],
|
||||||
];
|
];
|
||||||
const syntaxTreeContentProvider = new SyntaxTreeContentProvider();
|
const syntaxTreeContentProvider = new SyntaxTreeContentProvider();
|
||||||
const expandMacroContentProvider = new ExpandMacroContentProvider();
|
const expandMacroContentProvider = new ExpandMacroContentProvider();
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import * as vscode from 'vscode';
|
import * as vscode from 'vscode';
|
||||||
import * as lc from 'vscode-languageclient';
|
import * as lc from 'vscode-languageclient';
|
||||||
|
|
||||||
import { Server } from '../server';
|
import { Ctx } from './ctx';
|
||||||
|
|
||||||
export interface SourceChange {
|
export interface SourceChange {
|
||||||
label: string;
|
label: string;
|
||||||
|
@ -9,8 +9,8 @@ export interface SourceChange {
|
||||||
cursorPosition?: lc.TextDocumentPositionParams;
|
cursorPosition?: lc.TextDocumentPositionParams;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function handle(change: SourceChange) {
|
export async function applySourceChange(ctx: Ctx, change: SourceChange) {
|
||||||
const wsEdit = Server.client.protocol2CodeConverter.asWorkspaceEdit(
|
const wsEdit = ctx.client.protocol2CodeConverter.asWorkspaceEdit(
|
||||||
change.workspaceEdit,
|
change.workspaceEdit,
|
||||||
);
|
);
|
||||||
let created;
|
let created;
|
||||||
|
@ -32,10 +32,10 @@ export async function handle(change: SourceChange) {
|
||||||
const doc = await vscode.workspace.openTextDocument(toOpenUri);
|
const doc = await vscode.workspace.openTextDocument(toOpenUri);
|
||||||
await vscode.window.showTextDocument(doc);
|
await vscode.window.showTextDocument(doc);
|
||||||
} else if (toReveal) {
|
} else if (toReveal) {
|
||||||
const uri = Server.client.protocol2CodeConverter.asUri(
|
const uri = ctx.client.protocol2CodeConverter.asUri(
|
||||||
toReveal.textDocument.uri,
|
toReveal.textDocument.uri,
|
||||||
);
|
);
|
||||||
const position = Server.client.protocol2CodeConverter.asPosition(
|
const position = ctx.client.protocol2CodeConverter.asPosition(
|
||||||
toReveal.position,
|
toReveal.position,
|
||||||
);
|
);
|
||||||
const editor = vscode.window.activeTextEditor;
|
const editor = vscode.window.activeTextEditor;
|
Loading…
Reference in a new issue