Skip to content

Commit 95efe1f

Browse files
committed
feat: support acceptKind for text editor selections
1 parent 7ac6cf7 commit 95efe1f

File tree

4 files changed

+20
-31
lines changed

4 files changed

+20
-31
lines changed

docs/guide/editor.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ The following 4 composable can be used to **get and set** the selections of edit
124124
- `reactive::useNotebookEditorSelections` - All selections in the notebook editor.
125125
- `reactive::useNotebookEditorSelection` - The primary selection in the notebook editor.
126126

127-
See their docs for more information.
127+
See their docs for more information. Note that `reactive::useTextEditorSelections` and `reactive::useTextEditorSelection` also support an `acceptKind` option to filter the change kind which has triggered this event (See `vscode::TextEditorSelectionChangeKind`).
128128

129129
## Editor Viewport
130130

packages/core/src/composables/useNotebookEditorSelection.ts

+5-12
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,22 @@
11
import type { NotebookEditor } from 'vscode'
2-
import { window } from 'vscode'
3-
import { computed, shallowRef } from '../reactivity'
2+
import { computed } from '../reactivity'
43
import { createKeyedComposable } from '../utils'
5-
import { useDisposable } from './useDisposable'
4+
import { useNotebookEditorSelections } from './useNotebookEditorSelections'
65

76
/**
87
* @reactive `NotebookEditor.selection`
98
* @category editor
109
*/
1110
export const useNotebookEditorSelection = createKeyedComposable(
1211
(notebookEditor: NotebookEditor) => {
13-
const selection = shallowRef(notebookEditor.selection)
14-
15-
useDisposable(window.onDidChangeNotebookEditorSelection((ev) => {
16-
if (ev.notebookEditor === notebookEditor)
17-
selection.value = ev.selections[0]
18-
}))
12+
const selections = useNotebookEditorSelections(notebookEditor)
1913

2014
return computed({
2115
get() {
22-
return selection.value
16+
return selections.value[0]
2317
},
2418
set(newSelection) {
25-
selection.value = newSelection
26-
notebookEditor.selection = newSelection
19+
selections.value = selections.value.toSpliced(0, 1, newSelection)
2720
},
2821
})
2922
},

packages/core/src/composables/useTextEditorSelection.ts

+8-14
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,23 @@
1-
import type { TextEditor } from 'vscode'
2-
import { window } from 'vscode'
3-
import { computed, shallowRef } from '../reactivity'
1+
import type { TextEditor, TextEditorSelectionChangeKind } from 'vscode'
2+
import type { MaybeRefOrGetter } from '../reactivity'
3+
import { computed } from '../reactivity'
44
import { createKeyedComposable } from '../utils'
5-
import { useDisposable } from './useDisposable'
5+
import { useTextEditorSelections } from './useTextEditorSelections'
66

77
/**
88
* @reactive `TextEditor.selection`
99
* @category editor
1010
*/
1111
export const useTextEditorSelection = createKeyedComposable(
12-
(textEditor: TextEditor) => {
13-
const selection = shallowRef(textEditor.selection)
14-
15-
useDisposable(window.onDidChangeTextEditorSelection((ev) => {
16-
if (ev.textEditor === textEditor)
17-
selection.value = ev.selections[0]
18-
}))
12+
(textEditor: TextEditor, acceptKind?: MaybeRefOrGetter<(TextEditorSelectionChangeKind | undefined)[]>) => {
13+
const selections = useTextEditorSelections(textEditor, acceptKind)
1914

2015
return computed({
2116
get() {
22-
return selection.value
17+
return selections.value[0]
2318
},
2419
set(newSelection) {
25-
selection.value = newSelection
26-
textEditor.selection = newSelection
20+
selections.value = selections.value.toSpliced(0, 1, newSelection)
2721
},
2822
})
2923
},

packages/core/src/composables/useTextEditorSelections.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import type { TextEditor } from 'vscode'
1+
import type { TextEditor, TextEditorSelectionChangeKind } from 'vscode'
22
import { window } from 'vscode'
3-
import { computed, shallowRef } from '../reactivity'
3+
import type { MaybeRefOrGetter } from '../reactivity'
4+
import { computed, shallowRef, toValue } from '../reactivity'
45
import { createKeyedComposable } from '../utils'
56
import { useDisposable } from './useDisposable'
67

@@ -9,11 +10,12 @@ import { useDisposable } from './useDisposable'
910
* @category editor
1011
*/
1112
export const useTextEditorSelections = createKeyedComposable(
12-
(textEditor: TextEditor) => {
13+
(textEditor: TextEditor, acceptKind?: MaybeRefOrGetter<(TextEditorSelectionChangeKind | undefined)[]>) => {
1314
const selections = shallowRef(textEditor.selections)
1415

1516
useDisposable(window.onDidChangeTextEditorSelection((ev) => {
16-
if (ev.textEditor === textEditor)
17+
const kinds = toValue(acceptKind)
18+
if (ev.textEditor === textEditor && (!kinds || kinds.includes(ev.kind)))
1719
selections.value = ev.selections
1820
}))
1921

0 commit comments

Comments
 (0)