Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support reactive decorationTypeOrOptions #21

Merged
merged 1 commit into from
Feb 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { useEditorDecorations } from './useEditorDecorations'
* @category editor
*/
export function useActiveEditorDecorations(
decorationTypeOrOptions: TextEditorDecorationType | DecorationRenderOptions,
decorationTypeOrOptions: MaybeRefOrGetter<TextEditorDecorationType | DecorationRenderOptions>,
rangesOrOptions: MaybeRefOrGetter<readonly Range[] | readonly DecorationOptions[]>,
) {
const activeEditor = useActiveTextEditor()
Expand Down
18 changes: 12 additions & 6 deletions packages/core/src/composables/useEditorDecorations.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { MaybeRef, MaybeRefOrGetter } from '@reactive-vscode/reactivity'
import type { DecorationOptions, DecorationRenderOptions, Range, TextEditor, TextEditorDecorationType } from 'vscode'
import type { Awaitable, Nullable } from '../utils/types'
import { toValue, watch, watchEffect } from '@reactive-vscode/reactivity'
import { computed, toValue, watch, watchEffect } from '@reactive-vscode/reactivity'
import { window } from 'vscode'
import { useDisposable } from './useDisposable'
import { useDocumentText } from './useDocumentText'
Expand All @@ -22,7 +22,7 @@ export interface UseEditorDecorationsOptions {
*/
export function useEditorDecorations(
editor: MaybeRefOrGetter<Nullable<TextEditor>>,
decorationTypeOrOptions: TextEditorDecorationType | DecorationRenderOptions,
decorationTypeOrOptions: MaybeRefOrGetter<TextEditorDecorationType | DecorationRenderOptions>,
decorations:
| MaybeRef<readonly Range[] | readonly DecorationOptions[]>
| ((editor: TextEditor) => Awaitable<readonly Range[] | readonly DecorationOptions[]>),
Expand All @@ -32,17 +32,23 @@ export function useEditorDecorations(
updateOn = ['effect', 'documentChanged'],
} = options

const decorationType = 'key' in decorationTypeOrOptions
? decorationTypeOrOptions
: useDisposable(window.createTextEditorDecorationType(decorationTypeOrOptions))
const decorationType = computed<TextEditorDecorationType>(
(oldDecorationTypeOrOptions) => {
oldDecorationTypeOrOptions?.dispose()
const decorationTypeOrOptionsValue = toValue(decorationTypeOrOptions)
return 'key' in decorationTypeOrOptionsValue
? decorationTypeOrOptionsValue
: useDisposable(window.createTextEditorDecorationType(decorationTypeOrOptionsValue))
},
)

const update = async () => {
const editorValue = toValue(editor)
if (!editorValue)
return

editorValue.setDecorations(
decorationType,
decorationType.value,
typeof decorations === 'function'
? await decorations(editorValue)
: toValue(decorations),
Expand Down