From 10df138d6084000659626aaf50afb16e6b674b25 Mon Sep 17 00:00:00 2001 From: Kristóf Marussy Date: Fri, 5 Nov 2021 19:17:30 +0100 Subject: chore(web): implicit completion info in grammar Move information about which tokens should support implicit completions into the Lezer grammar. --- language-web/package.json | 1 + language-web/src/main/js/language/problem.grammar | 10 +++++++--- language-web/src/main/js/language/props.ts | 7 +++++++ language-web/src/main/js/xtext/ContentAssistService.ts | 15 ++++----------- language-web/yarn.lock | 2 +- 5 files changed, 20 insertions(+), 15 deletions(-) create mode 100644 language-web/src/main/js/language/props.ts diff --git a/language-web/package.json b/language-web/package.json index cb860c5f..3362a47a 100644 --- a/language-web/package.json +++ b/language-web/package.json @@ -85,6 +85,7 @@ "escape-string-regexp": "^5.0.0", "@fontsource/jetbrains-mono": "^4.5.0", "@fontsource/roboto": "^4.5.1", + "@lezer/common": "^0.15.7", "@lezer/lr": "^0.15.4", "loglevel": "^1.7.1", "loglevel-plugin-prefix": "^0.8.4", diff --git a/language-web/src/main/js/language/problem.grammar b/language-web/src/main/js/language/problem.grammar index 86cc7722..8e39243f 100644 --- a/language-web/src/main/js/language/problem.grammar +++ b/language-web/src/main/js/language/problem.grammar @@ -1,3 +1,7 @@ +@detectDelim + +@external prop implicitCompletion from '../../../../src/main/js/language/props.ts' + @top Problem { statement* } statement { @@ -89,11 +93,11 @@ VariableName { QualifiedName } NodeName { QualifiedName } -QualifiedName { identifier ("::" identifier)* } +QualifiedName[implicitCompletion=true] { identifier ("::" identifier)* } -kw { @specialize[@name={term}] } +kw { @specialize[@name={term},implicitCompletion=true] } -ckw { @extend[@name={term}] } +ckw { @extend[@name={term},implicitCompletion=true] } ParameterList { "(" sep<",", content> ")" } diff --git a/language-web/src/main/js/language/props.ts b/language-web/src/main/js/language/props.ts new file mode 100644 index 00000000..8e488bf5 --- /dev/null +++ b/language-web/src/main/js/language/props.ts @@ -0,0 +1,7 @@ +import { NodeProp } from '@lezer/common'; + +export const implicitCompletion = new NodeProp({ + deserialize(s: string) { + return s === 'true'; + }, +}); diff --git a/language-web/src/main/js/xtext/ContentAssistService.ts b/language-web/src/main/js/xtext/ContentAssistService.ts index 65381b21..aa9a80b0 100644 --- a/language-web/src/main/js/xtext/ContentAssistService.ts +++ b/language-web/src/main/js/xtext/ContentAssistService.ts @@ -7,18 +7,11 @@ import { syntaxTree } from '@codemirror/language'; import type { Transaction } from '@codemirror/state'; import escapeStringRegexp from 'escape-string-regexp'; +import { implicitCompletion } from '../language/props'; import type { UpdateService } from './UpdateService'; import { getLogger } from '../utils/logger'; import type { IContentAssistEntry } from './xtextServiceResults'; -const IMPLICIT_COMPLETION_TOKENS = [ - 'QualifiedName', - 'true', - 'false', - 'unknown', - 'error', -]; - const PROPOSALS_LIMIT = 1000; const IDENTIFIER_REGEXP_STR = '[a-zA-Z0-9_]*'; @@ -32,7 +25,7 @@ interface IFoundToken { to: number; - name: string; + implicitCompletion: boolean; text: string; } @@ -50,14 +43,14 @@ function findToken({ pos, state }: CompletionContext): IFoundToken | null { return { from: token.from, to: token.to, - name: token.name, + implicitCompletion: token.type.prop(implicitCompletion) || false, text: state.sliceDoc(token.from, token.to), }; } function shouldCompleteImplicitly(token: IFoundToken | null, context: CompletionContext): boolean { return token !== null - && IMPLICIT_COMPLETION_TOKENS.includes(token.name) + && token.implicitCompletion && context.pos - token.from >= 2; } diff --git a/language-web/yarn.lock b/language-web/yarn.lock index e05f4fa6..06b30508 100644 --- a/language-web/yarn.lock +++ b/language-web/yarn.lock @@ -1476,7 +1476,7 @@ resolved "https://registry.yarnpkg.com/@humanwhocodes/object-schema/-/object-schema-1.2.0.tgz#87de7af9c231826fdd68ac7258f77c429e0e5fcf" integrity sha512-wdppn25U8z/2yiaT6YGquE6X8sSv7hNMWSXYSSU1jGv/yd6XqjXgTDJ8KP4NgjTXfJ3GbRjeeb8RTV7a/VpM+w== -"@lezer/common@^0.15.0", "@lezer/common@^0.15.5": +"@lezer/common@^0.15.0", "@lezer/common@^0.15.5", "@lezer/common@^0.15.7": version "0.15.7" resolved "https://registry.yarnpkg.com/@lezer/common/-/common-0.15.7.tgz#8b445dae9777f689783132cf490770ece3c03d7b" integrity sha512-Rw8TDJnBzZnkyzIXs1Tmmd241FrBLJBj8gkdy3y0joGFb8Z4I/joKEsR+gv1pb13o1TMsZxm3fmP+d/wPt2CTQ== -- cgit v1.2.3-54-g00ecf