1
1
import type { MaybeRef , MaybeRefOrGetter } from '@reactive-vscode/reactivity'
2
2
import type { DecorationOptions , DecorationRenderOptions , Range , TextEditor , TextEditorDecorationType } from 'vscode'
3
3
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'
5
5
import { window } from 'vscode'
6
6
import { useDisposable } from './useDisposable'
7
7
import { useDocumentText } from './useDocumentText'
@@ -22,7 +22,7 @@ export interface UseEditorDecorationsOptions {
22
22
*/
23
23
export function useEditorDecorations (
24
24
editor : MaybeRefOrGetter < Nullable < TextEditor > > ,
25
- decorationTypeOrOptions : TextEditorDecorationType | DecorationRenderOptions ,
25
+ decorationTypeOrOptions : MaybeRefOrGetter < TextEditorDecorationType | DecorationRenderOptions > ,
26
26
decorations :
27
27
| MaybeRef < readonly Range [ ] | readonly DecorationOptions [ ] >
28
28
| ( ( editor : TextEditor ) => Awaitable < readonly Range [ ] | readonly DecorationOptions [ ] > ) ,
@@ -32,17 +32,23 @@ export function useEditorDecorations(
32
32
updateOn = [ 'effect' , 'documentChanged' ] ,
33
33
} = options
34
34
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
+ )
38
44
39
45
const update = async ( ) => {
40
46
const editorValue = toValue ( editor )
41
47
if ( ! editorValue )
42
48
return
43
49
44
50
editorValue . setDecorations (
45
- decorationType ,
51
+ decorationType . value ,
46
52
typeof decorations === 'function'
47
53
? await decorations ( editorValue )
48
54
: toValue ( decorations ) ,
0 commit comments