From 2fb854ccdae6f1f12b60441e5c3b283bdc81fb0a Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 7 Aug 2018 18:28:30 +0300 Subject: :tada: extend selection --- code/native/src/lib.rs | 32 ++++++++++++++++++++++++++++++-- code/package.json | 6 +++++- code/src/main.ts | 15 +++++++++++++++ 3 files changed, 50 insertions(+), 3 deletions(-) (limited to 'code') diff --git a/code/native/src/lib.rs b/code/native/src/lib.rs index aae7ad2f3..cb304a141 100644 --- a/code/native/src/lib.rs +++ b/code/native/src/lib.rs @@ -3,6 +3,7 @@ extern crate neon; extern crate libeditor; use neon::prelude::*; +use libeditor::TextRange; pub struct Wrapper { inner: libeditor::File, @@ -19,8 +20,8 @@ declare_types! { } method syntaxTree(mut cx) { - let this = cx.this(); let tree = { + let this = cx.this(); let guard = cx.lock(); let wrapper = this.borrow(&guard); wrapper.inner.syntax_tree() @@ -29,8 +30,8 @@ declare_types! { } method highlight(mut cx) { - let this = cx.this(); let highlights = { + let this = cx.this(); let guard = cx.lock(); let wrapper = this.borrow(&guard); wrapper.inner.highlight() @@ -51,6 +52,33 @@ declare_types! { Ok(res.upcast()) } + + method extendSelection(mut cx) { + let from_offset = cx.argument::(0)?.value() as u32; + let to_offset = cx.argument::(1)?.value() as u32; + let text_range = TextRange::from_to(from_offset.into(), to_offset.into()); + let extended_range = { + let this = cx.this(); + let guard = cx.lock(); + let wrapper = this.borrow(&guard); + wrapper.inner.extend_selection(text_range) + }; + + match extended_range { + None => Ok(cx.null().upcast()), + Some(range) => { + let start: u32 = range.start().into(); + let end: u32 = range.end().into(); + let start = cx.number(start); + let end = cx.number(end); + let arr = cx.empty_array(); + arr.set(&mut cx, 0, start)?; + arr.set(&mut cx, 1, end)?; + Ok(arr.upcast()) + } + } + + } } } diff --git a/code/package.json b/code/package.json index 2f97bb5c0..66c4f266f 100644 --- a/code/package.json +++ b/code/package.json @@ -36,11 +36,15 @@ { "command": "libsyntax-rust.syntaxTree", "title": "Show Rust syntax tree" + }, + { + "command": "libsyntax-rust.extendSelection", + "title": "Rust Extend Selection" } ], "keybindings": [ { - "command": "libsyntax-rust.semanticSelection", + "command": "libsyntax-rust.extendSelection", "key": "ctrl+w", "when": "editorTextFocus && editorLangId == rust" } diff --git a/code/src/main.ts b/code/src/main.ts index 75a824b7b..72bef7061 100644 --- a/code/src/main.ts +++ b/code/src/main.ts @@ -34,6 +34,16 @@ export function activate(context: vscode.ExtensionContext) { )) registerCommand('libsyntax-rust.syntaxTree', () => openDoc(uris.syntaxTree)) + registerCommand('libsyntax-rust.extendSelection', () => { + let editor = vscode.window.activeTextEditor + let file = activeSyntax() + if (editor == null || file == null) return + editor.selections = editor.selections.map((s) => { + let range = file.extendSelection(s) + if (range == null) return null + return new vscode.Selection(range.start, range.end) + }) + }) } export function deactivate() { } @@ -49,6 +59,11 @@ export class Syntax { syntaxTree(): string { return this.imp.syntaxTree() } highlight(): Array<[number, number, string]> { return this.imp.highlight() } + extendSelection(range: vscode.Range): vscode.Range { + let range_ = fromVsRange(this.doc, range); + let extRange = this.imp.extendSelection(range_[0], range_[1]); + return toVsRange(this.doc, extRange); + } } -- cgit v1.2.3