refactor: init environment highlighter
This commit is contained in:
@@ -33,7 +33,10 @@ import { isJSONContentType } from "../utils/contenttypes"
|
|||||||
import { Completer } from "./completion"
|
import { Completer } from "./completion"
|
||||||
import { LinterDefinition } from "./linting/linter"
|
import { LinterDefinition } from "./linting/linter"
|
||||||
import { basicSetup, baseTheme, baseHighlightStyle } from "./themes/baseTheme"
|
import { basicSetup, baseTheme, baseHighlightStyle } from "./themes/baseTheme"
|
||||||
import { environmentTooltip } from "./extensions/environmentTooltip"
|
import {
|
||||||
|
environmentHighlightStyle,
|
||||||
|
environmentTooltip,
|
||||||
|
} from "./extensions/environmentTooltip"
|
||||||
|
|
||||||
type ExtendedEditorConfig = {
|
type ExtendedEditorConfig = {
|
||||||
mode: string
|
mode: string
|
||||||
@@ -176,6 +179,7 @@ export function useCodemirror(
|
|||||||
baseTheme,
|
baseTheme,
|
||||||
baseHighlightStyle,
|
baseHighlightStyle,
|
||||||
environmentTooltip,
|
environmentTooltip,
|
||||||
|
environmentHighlightStyle,
|
||||||
ViewPlugin.fromClass(
|
ViewPlugin.fromClass(
|
||||||
class {
|
class {
|
||||||
update(update: ViewUpdate) {
|
update(update: ViewUpdate) {
|
||||||
|
|||||||
@@ -1,5 +1,14 @@
|
|||||||
import { Extension } from "@codemirror/state"
|
import { Extension } from "@codemirror/state"
|
||||||
import { hoverTooltip } from "@codemirror/tooltip"
|
import { hoverTooltip } from "@codemirror/tooltip"
|
||||||
|
import {
|
||||||
|
Decoration,
|
||||||
|
EditorView,
|
||||||
|
MatchDecorator,
|
||||||
|
PluginField,
|
||||||
|
ViewPlugin,
|
||||||
|
ViewUpdate,
|
||||||
|
WidgetType,
|
||||||
|
} from "@codemirror/view"
|
||||||
import { useReadonlyStream } from "~/helpers/utils/composables"
|
import { useReadonlyStream } from "~/helpers/utils/composables"
|
||||||
import { aggregateEnvs$ } from "~/newstore/environments"
|
import { aggregateEnvs$ } from "~/newstore/environments"
|
||||||
|
|
||||||
@@ -11,8 +20,11 @@ const cursorTooltipField = hoverTooltip((view, pos, side) => {
|
|||||||
while (start > from && /\w/.test(text[start - from - 1])) start--
|
while (start > from && /\w/.test(text[start - from - 1])) start--
|
||||||
while (end < to && /\w/.test(text[end - from])) end++
|
while (end < to && /\w/.test(text[end - from])) end++
|
||||||
|
|
||||||
if ((start === pos && side < 0) || (end === pos && side > 0)) return null
|
if (
|
||||||
if (!/(<<\w+>>)/g.test(text.slice(start - from - 2, end - from + 2)))
|
(start === pos && side < 0) ||
|
||||||
|
(end === pos && side > 0) ||
|
||||||
|
!/(<<\w+>>)/g.test(text.slice(start - from - 2, end - from + 2))
|
||||||
|
)
|
||||||
return null
|
return null
|
||||||
|
|
||||||
const aggregateEnvs = useReadonlyStream(aggregateEnvs$, null)
|
const aggregateEnvs = useReadonlyStream(aggregateEnvs$, null)
|
||||||
@@ -52,4 +64,39 @@ function getEnvValue(value: string) {
|
|||||||
return "not found"
|
return "not found"
|
||||||
}
|
}
|
||||||
|
|
||||||
export const environmentTooltip: Extension = cursorTooltipField
|
const environmentDecorator = new MatchDecorator({
|
||||||
|
// eslint-disable-next-line prefer-regex-literals
|
||||||
|
regexp: new RegExp(/(<<\w+>>)/g),
|
||||||
|
decoration: () =>
|
||||||
|
Decoration.replace({
|
||||||
|
widget: new (class extends WidgetType {
|
||||||
|
toDOM(_view: EditorView) {
|
||||||
|
const element = document.createElement("span")
|
||||||
|
element.textContent = "view"
|
||||||
|
return element
|
||||||
|
}
|
||||||
|
})(),
|
||||||
|
inclusive: false,
|
||||||
|
}),
|
||||||
|
})
|
||||||
|
|
||||||
|
export const environmentHighlightStyle = ViewPlugin.define(
|
||||||
|
(view: EditorView) => ({
|
||||||
|
decorations: environmentDecorator.createDeco(view),
|
||||||
|
update(_update: ViewUpdate) {
|
||||||
|
this.decorations = environmentDecorator.updateDeco(
|
||||||
|
_update,
|
||||||
|
this.decorations
|
||||||
|
)
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
{
|
||||||
|
decorations: (v) => v.decorations,
|
||||||
|
provide: PluginField.atomicRanges.from((v) => v.decorations),
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
export const environmentTooltip: Extension = [
|
||||||
|
cursorTooltipField,
|
||||||
|
environmentHighlightStyle,
|
||||||
|
]
|
||||||
|
|||||||
Reference in New Issue
Block a user