Skip to content

Commit

Permalink
feat(expressions): support token doc (#1874)
Browse files Browse the repository at this point in the history
  • Loading branch information
Leopoldthecoder authored Dec 26, 2024
1 parent 34eb520 commit 68c525a
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 7 deletions.
8 changes: 8 additions & 0 deletions packages/core/expressions/docs/expressions-editor.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ To control whether the editor should be inactive until its initial focus.

To control whether the editor should pass validation with an empty input.

#### `defaultShowDetails`

- type: `boolean`
- required: `false`
- default: `false`

To control whether the editor should show the details of the autocompletion item by default.

#### `editorOptions`

- type: `Monaco.editor.IEditorOptions`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const props = withDefaults(defineProps<{
parseDebounce?: number,
inactiveUntilFocused?: boolean,
allowEmptyInput?: boolean,
defaultShowDetails?: boolean,
editorOptions?: Monaco.editor.IEditorOptions
}>(), {
parseDebounce: 500,
Expand Down Expand Up @@ -80,6 +81,10 @@ onMounted(() => {
...props.editorOptions,
})
if (props.defaultShowDetails) {
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
12 changes: 8 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,7 @@ const buildTokenTree = (schemaDefinition: SchemaDefinition) => {
token = token.children[t]
}
token.detail = kind
token.documentation = documentation?.[field]
}
}

Expand Down Expand Up @@ -61,7 +64,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 @@ -154,6 +157,7 @@ export const registerLanguage = (schema: Schema) => {
label: token === '*' ? '...' : token,
kind: monaco.languages.CompletionItemKind.Property,
detail: props.detail,
documentation: props.documentation,
insertText: token === '*' ? '' : token,
range,
})),
Expand Down
7 changes: 4 additions & 3 deletions packages/core/expressions/src/schema.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { Schema as AtcSchema, type AstType } from '@kong/atc-router'

export type SchemaDefinition = {
[K in AstType]?: string[];
[K in AstType]?: string[]
}

export interface Schema {
name: string;
definition: SchemaDefinition;
name: string
definition: SchemaDefinition
documentation?: Record<string, string>
}

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

0 comments on commit 68c525a

Please sign in to comment.