Skip to content

Commit

Permalink
feat(expressions): support token doc
Browse files Browse the repository at this point in the history
  • Loading branch information
Leopoldthecoder committed Dec 26, 2024
1 parent a0bf3fe commit db93b60
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ onMounted(() => {
...props.editorOptions,
})
editor.getContribution<Record<string, any> & Monaco.editor.IEditorContribution>('editor.contrib.suggestController')?.widget?.value._setDetailsVisible(true)
editor.onDidChangeModelContent(() => {
const value = editor!.getValue()
expression.value = value
Expand Down
14 changes: 10 additions & 4 deletions packages/core/expressions/src/monaco.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as monaco from 'monaco-editor'
import { type Schema, type SchemaDefinition } from './schema'
import { type Schema } from './schema'

interface MonarchLanguage extends monaco.languages.IMonarchLanguage {
keywords: string[];
Expand All @@ -8,12 +8,14 @@ interface MonarchLanguage extends monaco.languages.IMonarchLanguage {
interface Token {
detail?: string;
children?: Record<string, Token>;
documentation?: string;
}

const buildTokenTree = (schemaDefinition: SchemaDefinition) => {
const buildTokenTree = (schema: Schema) => {
const { definition, documentation } = schema
const root: Token = {}

for (const [kind, fields] of Object.entries(schemaDefinition)) {
for (const [kind, fields] of Object.entries(definition)) {
for (const field of fields) {
let token = root
for (const t of field.split('.')) {
Expand All @@ -26,6 +28,9 @@ const buildTokenTree = (schemaDefinition: SchemaDefinition) => {
token = token.children[t]
}
token.detail = kind
if (documentation?.[field] !== undefined) {
token.documentation = documentation[field]
}
}
}

Expand Down Expand Up @@ -61,7 +66,7 @@ export const registerLanguage = (schema: Schema) => {
return { languageId }
}

const tokenTree = buildTokenTree(schema.definition)
const tokenTree = buildTokenTree(schema)

const keywords = ['not', 'in', 'contains']

Expand Down Expand Up @@ -153,6 +158,7 @@ export const registerLanguage = (schema: Schema) => {
detail: props.detail,
insertText: token === '*' ? '' : token,
range,
documentation: props.documentation,
})),
}
},
Expand Down
1 change: 1 addition & 0 deletions packages/core/expressions/src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export type SchemaDefinition = {
export interface Schema {
name: string;
definition: SchemaDefinition;
documentation?: Record<string, string>;
}

export const createSchema = (definition: SchemaDefinition) => {
Expand Down

0 comments on commit db93b60

Please sign in to comment.