Skip to content

Commit f64ace0

Browse files
feat: support reactive decorationTypeOrOptions (#21)
1 parent 07f02d7 commit f64ace0

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

packages/core/src/composables/useActiveEditorDecorations.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { useEditorDecorations } from './useEditorDecorations'
99
* @category editor
1010
*/
1111
export function useActiveEditorDecorations(
12-
decorationTypeOrOptions: TextEditorDecorationType | DecorationRenderOptions,
12+
decorationTypeOrOptions: MaybeRefOrGetter<TextEditorDecorationType | DecorationRenderOptions>,
1313
rangesOrOptions: MaybeRefOrGetter<readonly Range[] | readonly DecorationOptions[]>,
1414
) {
1515
const activeEditor = useActiveTextEditor()

packages/core/src/composables/useEditorDecorations.ts

+12-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { MaybeRef, MaybeRefOrGetter } from '@reactive-vscode/reactivity'
22
import type { DecorationOptions, DecorationRenderOptions, Range, TextEditor, TextEditorDecorationType } from 'vscode'
33
import type { Awaitable, Nullable } from '../utils/types'
4-
import { toValue, watch, watchEffect } from '@reactive-vscode/reactivity'
4+
import { computed, toValue, watch, watchEffect } from '@reactive-vscode/reactivity'
55
import { window } from 'vscode'
66
import { useDisposable } from './useDisposable'
77
import { useDocumentText } from './useDocumentText'
@@ -22,7 +22,7 @@ export interface UseEditorDecorationsOptions {
2222
*/
2323
export function useEditorDecorations(
2424
editor: MaybeRefOrGetter<Nullable<TextEditor>>,
25-
decorationTypeOrOptions: TextEditorDecorationType | DecorationRenderOptions,
25+
decorationTypeOrOptions: MaybeRefOrGetter<TextEditorDecorationType | DecorationRenderOptions>,
2626
decorations:
2727
| MaybeRef<readonly Range[] | readonly DecorationOptions[]>
2828
| ((editor: TextEditor) => Awaitable<readonly Range[] | readonly DecorationOptions[]>),
@@ -32,17 +32,23 @@ export function useEditorDecorations(
3232
updateOn = ['effect', 'documentChanged'],
3333
} = options
3434

35-
const decorationType = 'key' in decorationTypeOrOptions
36-
? decorationTypeOrOptions
37-
: useDisposable(window.createTextEditorDecorationType(decorationTypeOrOptions))
35+
const decorationType = computed<TextEditorDecorationType>(
36+
(oldDecorationTypeOrOptions) => {
37+
oldDecorationTypeOrOptions?.dispose()
38+
const decorationTypeOrOptionsValue = toValue(decorationTypeOrOptions)
39+
return 'key' in decorationTypeOrOptionsValue
40+
? decorationTypeOrOptionsValue
41+
: useDisposable(window.createTextEditorDecorationType(decorationTypeOrOptionsValue))
42+
},
43+
)
3844

3945
const update = async () => {
4046
const editorValue = toValue(editor)
4147
if (!editorValue)
4248
return
4349

4450
editorValue.setDecorations(
45-
decorationType,
51+
decorationType.value,
4652
typeof decorations === 'function'
4753
? await decorations(editorValue)
4854
: toValue(decorations),

0 commit comments

Comments
 (0)