2687: Move matching brace to new Ctx r=matklad a=matklad



Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
bors[bot] 2019-12-30 14:26:07 +00:00 committed by GitHub
commit 28ef2ea4f9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 110 additions and 52 deletions

@ -534,6 +534,12 @@
"integrity": "sha1-elfrVQpng/kRUzH89GY9XI4AelA=",
"dev": true
},
"prettier": {
"version": "1.19.1",
"resolved": "https://registry.npmjs.org/prettier/-/prettier-1.19.1.tgz",
"integrity": "sha512-s7PoyDv/II1ObgQunCbB9PdLmUcBZcnWOcxDh7O0N/UwDEsHyqkW+Qh28jW+mVuCdx7gLB0BotYI1Y6uI9iyew==",
"dev": true
},
"read": {
"version": "1.0.7",
"resolved": "https://registry.npmjs.org/read/-/read-1.0.7.tgz",

@ -18,7 +18,8 @@
"scripts": {
"vscode:prepublish": "rollup -c",
"package": "vsce package",
"watch": "tsc -watch -p ./"
"watch": "tsc -watch -p ./",
"prettier": "prettier --write **/*.ts"
},
"dependencies": {
"jsonc-parser": "^2.1.0",
@ -26,16 +27,22 @@
"vscode-languageclient": "^6.0.0-next.9"
},
"devDependencies": {
"rollup": "^1.27.14",
"@rollup/plugin-commonjs": "^11.0.0",
"@rollup/plugin-node-resolve": "^6.0.0",
"@rollup/plugin-typescript": "^2.0.1",
"typescript": "^3.7.3",
"tslib": "^1.10.0",
"vsce": "^1.71.0",
"@types/node": "^12.12.21",
"@types/seedrandom": "^2.4.28",
"@types/vscode": "^1.41.0"
"@types/vscode": "^1.41.0",
"prettier": "^1.19.1",
"rollup": "^1.27.14",
"tslib": "^1.10.0",
"typescript": "^3.7.3",
"vsce": "^1.71.0"
},
"prettier": {
"singleQuote": true,
"tabWidth": 4,
"trailingComma": "all"
},
"activationEvents": [
"onLanguage:rust",

@ -1,19 +1,19 @@
import * as vscode from 'vscode';
import { Server } from '../server';
import { Ctx, Cmd } from '../ctx';
// Shows status of rust-analyzer (for debugging)
export function makeCommand(context: vscode.ExtensionContext) {
export function analyzerStatus(ctx: Ctx): Cmd {
let poller: NodeJS.Timer | null = null;
const tdcp = new TextDocumentContentProvider();
const tdcp = new TextDocumentContentProvider(ctx);
context.subscriptions.push(
ctx.pushCleanup(
vscode.workspace.registerTextDocumentContentProvider(
'rust-analyzer-status',
tdcp,
),
);
context.subscriptions.push({
ctx.pushCleanup({
dispose() {
if (poller != null) {
clearInterval(poller);
@ -39,9 +39,16 @@ export function makeCommand(context: vscode.ExtensionContext) {
class TextDocumentContentProvider
implements vscode.TextDocumentContentProvider {
uri = vscode.Uri.parse('rust-analyzer-status://status');
eventEmitter = new vscode.EventEmitter<vscode.Uri>();
ctx: Ctx
constructor(ctx: Ctx) {
this.ctx = ctx
}
provideTextDocumentContent(
_uri: vscode.Uri,
): vscode.ProviderResult<string> {
@ -49,7 +56,7 @@ class TextDocumentContentProvider
if (editor == null) {
return '';
}
return Server.client.sendRequest<string>(
return this.ctx.client.sendRequest<string>(
'rust-analyzer/analyzerStatus',
null,
);

@ -1,14 +1,20 @@
import * as analyzerStatus from './analyzer_status';
import { Ctx, Cmd } from '../ctx'
import { analyzerStatus } from './analyzer_status';
import { matchingBrace } from './matching_brace';
import * as applySourceChange from './apply_source_change';
import * as expandMacro from './expand_macro';
import * as inlayHints from './inlay_hints';
import * as joinLines from './join_lines';
import * as matchingBrace from './matching_brace';
import * as onEnter from './on_enter';
import * as parentModule from './parent_module';
import * as runnables from './runnables';
import * as syntaxTree from './syntaxTree';
function collectGarbage(ctx: Ctx): Cmd {
return async () => { ctx.client.sendRequest<null>('rust-analyzer/collectGarbage', null) }
}
export {
analyzerStatus,
applySourceChange,
@ -20,4 +26,5 @@ export {
syntaxTree,
onEnter,
inlayHints,
collectGarbage
};

@ -1,34 +1,33 @@
import * as vscode from 'vscode';
import { Position, TextDocumentIdentifier } from 'vscode-languageclient';
import { Server } from '../server';
import { Ctx, Cmd } from '../ctx';
export function matchingBrace(ctx: Ctx): Cmd {
return async () => {
const editor = ctx.activeRustEditor;
if (!editor) {
return;
}
const request: FindMatchingBraceParams = {
textDocument: { uri: editor.document.uri.toString() },
offsets: editor.selections.map(s => ctx.client.code2ProtocolConverter.asPosition(s.active)),
};
const response = await ctx.client.sendRequest<Position[]>(
'rust-analyzer/findMatchingBrace',
request,
);
editor.selections = editor.selections.map((sel, idx) => {
const active = ctx.client.protocol2CodeConverter.asPosition(
response[idx],
);
const anchor = sel.isEmpty ? active : sel.anchor;
return new vscode.Selection(anchor, active);
});
editor.revealRange(editor.selection);
}
}
interface FindMatchingBraceParams {
textDocument: TextDocumentIdentifier;
offsets: Position[];
}
export async function handle() {
const editor = vscode.window.activeTextEditor;
if (editor == null || editor.document.languageId !== 'rust') {
return;
}
const request: FindMatchingBraceParams = {
textDocument: { uri: editor.document.uri.toString() },
offsets: editor.selections.map(s => {
return Server.client.code2ProtocolConverter.asPosition(s.active);
}),
};
const response = await Server.client.sendRequest<Position[]>(
'rust-analyzer/findMatchingBrace',
request,
);
editor.selections = editor.selections.map((sel, idx) => {
const active = Server.client.protocol2CodeConverter.asPosition(
response[idx],
);
const anchor = sel.isEmpty ? active : sel.anchor;
return new vscode.Selection(anchor, active);
});
editor.revealRange(editor.selection);
}

35
editors/code/src/ctx.ts Normal file

@ -0,0 +1,35 @@
import * as vscode from 'vscode';
import * as lc from 'vscode-languageclient';
import { Server } from './server';
export class Ctx {
private extCtx: vscode.ExtensionContext;
constructor(extCtx: vscode.ExtensionContext) {
this.extCtx = extCtx;
}
get client(): lc.LanguageClient {
return Server.client;
}
get activeRustEditor(): vscode.TextEditor | undefined {
const editor = vscode.window.activeTextEditor;
return editor && editor.document.languageId === 'rust'
? editor
: undefined;
}
registerCommand(name: string, factory: (ctx: Ctx) => Cmd) {
const fullName = `rust-analyzer.${name}`;
const cmd = factory(this);
const d = vscode.commands.registerCommand(fullName, cmd);
this.pushCleanup(d);
}
pushCleanup(d: { dispose(): any }) {
this.extCtx.subscriptions.push(d);
}
}
export type Cmd = (...args: any[]) => any;

@ -9,8 +9,16 @@ import { StatusDisplay } from './commands/watch_status';
import * as events from './events';
import * as notifications from './notifications';
import { Server } from './server';
import { Ctx } from './ctx';
let ctx!: Ctx;
export async function activate(context: vscode.ExtensionContext) {
ctx = new Ctx(context);
ctx.registerCommand('analyzerStatus', commands.analyzerStatus);
ctx.registerCommand('collectGarbage', commands.collectGarbage);
ctx.registerCommand('matchingBrace', commands.matchingBrace);
function disposeOnDeactivation(disposable: vscode.Disposable) {
context.subscriptions.push(disposable);
}
@ -48,17 +56,6 @@ export async function activate(context: vscode.ExtensionContext) {
}
// Commands are requests from vscode to the language server
registerCommand(
'rust-analyzer.analyzerStatus',
commands.analyzerStatus.makeCommand(context),
);
registerCommand('rust-analyzer.collectGarbage', () =>
Server.client.sendRequest<null>('rust-analyzer/collectGarbage', null),
);
registerCommand(
'rust-analyzer.matchingBrace',
commands.matchingBrace.handle,
);
registerCommand('rust-analyzer.joinLines', commands.joinLines.handle);
registerCommand('rust-analyzer.parentModule', commands.parentModule.handle);
registerCommand('rust-analyzer.run', commands.runnables.handle);