diff options
Diffstat (limited to 'code')
-rw-r--r-- | code/native/src/lib.rs | 32 | ||||
-rw-r--r-- | code/package.json | 6 | ||||
-rw-r--r-- | code/src/main.ts | 15 |
3 files changed, 50 insertions, 3 deletions
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; | |||
3 | extern crate libeditor; | 3 | extern crate libeditor; |
4 | 4 | ||
5 | use neon::prelude::*; | 5 | use neon::prelude::*; |
6 | use libeditor::TextRange; | ||
6 | 7 | ||
7 | pub struct Wrapper { | 8 | pub struct Wrapper { |
8 | inner: libeditor::File, | 9 | inner: libeditor::File, |
@@ -19,8 +20,8 @@ declare_types! { | |||
19 | } | 20 | } |
20 | 21 | ||
21 | method syntaxTree(mut cx) { | 22 | method syntaxTree(mut cx) { |
22 | let this = cx.this(); | ||
23 | let tree = { | 23 | let tree = { |
24 | let this = cx.this(); | ||
24 | let guard = cx.lock(); | 25 | let guard = cx.lock(); |
25 | let wrapper = this.borrow(&guard); | 26 | let wrapper = this.borrow(&guard); |
26 | wrapper.inner.syntax_tree() | 27 | wrapper.inner.syntax_tree() |
@@ -29,8 +30,8 @@ declare_types! { | |||
29 | } | 30 | } |
30 | 31 | ||
31 | method highlight(mut cx) { | 32 | method highlight(mut cx) { |
32 | let this = cx.this(); | ||
33 | let highlights = { | 33 | let highlights = { |
34 | let this = cx.this(); | ||
34 | let guard = cx.lock(); | 35 | let guard = cx.lock(); |
35 | let wrapper = this.borrow(&guard); | 36 | let wrapper = this.borrow(&guard); |
36 | wrapper.inner.highlight() | 37 | wrapper.inner.highlight() |
@@ -51,6 +52,33 @@ declare_types! { | |||
51 | 52 | ||
52 | Ok(res.upcast()) | 53 | Ok(res.upcast()) |
53 | } | 54 | } |
55 | |||
56 | method extendSelection(mut cx) { | ||
57 | let from_offset = cx.argument::<JsNumber>(0)?.value() as u32; | ||
58 | let to_offset = cx.argument::<JsNumber>(1)?.value() as u32; | ||
59 | let text_range = TextRange::from_to(from_offset.into(), to_offset.into()); | ||
60 | let extended_range = { | ||
61 | let this = cx.this(); | ||
62 | let guard = cx.lock(); | ||
63 | let wrapper = this.borrow(&guard); | ||
64 | wrapper.inner.extend_selection(text_range) | ||
65 | }; | ||
66 | |||
67 | match extended_range { | ||
68 | None => Ok(cx.null().upcast()), | ||
69 | Some(range) => { | ||
70 | let start: u32 = range.start().into(); | ||
71 | let end: u32 = range.end().into(); | ||
72 | let start = cx.number(start); | ||
73 | let end = cx.number(end); | ||
74 | let arr = cx.empty_array(); | ||
75 | arr.set(&mut cx, 0, start)?; | ||
76 | arr.set(&mut cx, 1, end)?; | ||
77 | Ok(arr.upcast()) | ||
78 | } | ||
79 | } | ||
80 | |||
81 | } | ||
54 | } | 82 | } |
55 | 83 | ||
56 | } | 84 | } |
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 @@ | |||
36 | { | 36 | { |
37 | "command": "libsyntax-rust.syntaxTree", | 37 | "command": "libsyntax-rust.syntaxTree", |
38 | "title": "Show Rust syntax tree" | 38 | "title": "Show Rust syntax tree" |
39 | }, | ||
40 | { | ||
41 | "command": "libsyntax-rust.extendSelection", | ||
42 | "title": "Rust Extend Selection" | ||
39 | } | 43 | } |
40 | ], | 44 | ], |
41 | "keybindings": [ | 45 | "keybindings": [ |
42 | { | 46 | { |
43 | "command": "libsyntax-rust.semanticSelection", | 47 | "command": "libsyntax-rust.extendSelection", |
44 | "key": "ctrl+w", | 48 | "key": "ctrl+w", |
45 | "when": "editorTextFocus && editorLangId == rust" | 49 | "when": "editorTextFocus && editorLangId == rust" |
46 | } | 50 | } |
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) { | |||
34 | )) | 34 | )) |
35 | 35 | ||
36 | registerCommand('libsyntax-rust.syntaxTree', () => openDoc(uris.syntaxTree)) | 36 | registerCommand('libsyntax-rust.syntaxTree', () => openDoc(uris.syntaxTree)) |
37 | registerCommand('libsyntax-rust.extendSelection', () => { | ||
38 | let editor = vscode.window.activeTextEditor | ||
39 | let file = activeSyntax() | ||
40 | if (editor == null || file == null) return | ||
41 | editor.selections = editor.selections.map((s) => { | ||
42 | let range = file.extendSelection(s) | ||
43 | if (range == null) return null | ||
44 | return new vscode.Selection(range.start, range.end) | ||
45 | }) | ||
46 | }) | ||
37 | } | 47 | } |
38 | 48 | ||
39 | export function deactivate() { } | 49 | export function deactivate() { } |
@@ -49,6 +59,11 @@ export class Syntax { | |||
49 | 59 | ||
50 | syntaxTree(): string { return this.imp.syntaxTree() } | 60 | syntaxTree(): string { return this.imp.syntaxTree() } |
51 | highlight(): Array<[number, number, string]> { return this.imp.highlight() } | 61 | highlight(): Array<[number, number, string]> { return this.imp.highlight() } |
62 | extendSelection(range: vscode.Range): vscode.Range { | ||
63 | let range_ = fromVsRange(this.doc, range); | ||
64 | let extRange = this.imp.extendSelection(range_[0], range_[1]); | ||
65 | return toVsRange(this.doc, extRange); | ||
66 | } | ||
52 | } | 67 | } |
53 | 68 | ||
54 | 69 | ||