From b8a54c1f8379d2ad501f4f3bd685bc401f7de684 Mon Sep 17 00:00:00 2001 From: Fred Bricon Date: Thu, 19 Sep 2024 15:11:08 +0200 Subject: [PATCH] fix: Q:CONFIG syntax highlighting and completion Signed-off-by: Fred Bricon --- language-support/inline-jbang.tmLanguage.json | 2 +- src/JBangDirectives.ts | 2 +- src/completion/DirectivesCompletion.ts | 43 +++++++++---------- 3 files changed, 22 insertions(+), 25 deletions(-) diff --git a/language-support/inline-jbang.tmLanguage.json b/language-support/inline-jbang.tmLanguage.json index 1271cf0..13f5bf2 100644 --- a/language-support/inline-jbang.tmLanguage.json +++ b/language-support/inline-jbang.tmLanguage.json @@ -5,7 +5,7 @@ "patterns": [ { "contentName": "meta.embedded.block.jbang", - "match": "^(//)(DEPS|JAVA|MAIN|FILES|SOURCES|PREVIEW|MODULE|DESCRIPTION|GAV|COMPILE_OPTIONS|JAVAC_OPTIONS|RUNTIME_OPTIONS|JAVA_OPTIONS|NATIVE_OPTIONS|REPOS|MANIFEST|CDS|KOTLIN|GROOVY|JAVAAGENT)\\s+(.*)$", + "match": "^(//)(DEPS|JAVA|MAIN|FILES|SOURCES|PREVIEW|MODULE|DESCRIPTION|GAV|COMPILE_OPTIONS|JAVAC_OPTIONS|RUNTIME_OPTIONS|JAVA_OPTIONS|NATIVE_OPTIONS|REPOS|MANIFEST|CDS|KOTLIN|GROOVY|JAVAAGENT|Q:CONFIG+)\\s+(.*)$", "captures": { "0":{ "name":"directive.jbang" diff --git a/src/JBangDirectives.ts b/src/JBangDirectives.ts index eeb9052..365f399 100644 --- a/src/JBangDirectives.ts +++ b/src/JBangDirectives.ts @@ -33,7 +33,7 @@ export const PREVIEW = new Directive("PREVIEW","Enable Java preview features", f export const SOURCES = new Directive("SOURCES", "Pattern to include as JBang sources", true); export const FILES = new Directive("FILES", "Mount files to build", true); export const REPOS = new Directive("REPOS", "Repositories used by Jbang to resolve dependencies"); -export const QUARKUS_CONFIG = new Directive("Q:CONFIG ", "Quarkus configuration property"); +export const QUARKUS_CONFIG = new Directive("Q:CONFIG", "Quarkus configuration property"); //Special case export const FILES_PREFIX = `${FILES.prefix()} `; diff --git a/src/completion/DirectivesCompletion.ts b/src/completion/DirectivesCompletion.ts index 0cb6310..24e396e 100644 --- a/src/completion/DirectivesCompletion.ts +++ b/src/completion/DirectivesCompletion.ts @@ -8,7 +8,7 @@ const retriggerCompletion: Command = { command: 'editor.action.triggerSuggest', const QUARKUS_DEP = new Directive("DEPS io.quarkus:quarkus",""); //pretend it's a directive export class DirectivesCompletion implements CompletionParticipant { - + applies(lineText: string, position: Position): boolean { return lineText.startsWith('//') || position.character === 0; } @@ -31,7 +31,7 @@ export class DirectivesCompletion implements CompletionParticipant { items.push(getCompletion(JAVA, range)); } items.push(getCompletion(DEPS, range)); - + if (document.languageId === 'groovy' && !scanner.found(GROOVY)) { items.push(getCompletion(GROOVY, range)); } @@ -48,9 +48,9 @@ export class DirectivesCompletion implements CompletionParticipant { if (!scanner.found(MAIN)) { items.push(getCompletion(MAIN, range)); } - + items.push(getCompletion(SOURCES, range)); - + items.push(getCompletion(FILES, range)); items.push(getCompletion(REPOS, range)); @@ -93,7 +93,7 @@ export class DirectivesCompletion implements CompletionParticipant { class DirectiveScanner { - + directives:Directive[] = []; found(directive: Directive): boolean { @@ -101,28 +101,25 @@ class DirectiveScanner { } scan(document: TextDocument) { - const checkedDirectives = [ - JAVA, JAVAC_OPTIONS, COMPILE_OPTIONS, DESCRIPTION, CDS, GAV, JAVAAGENT, MANIFEST, JAVA_OPTIONS, RUNTIME_OPTIONS, NATIVE_OPTIONS, KOTLIN, GROOVY, MAIN, MODULE, PREVIEW, QUARKUS_DEP - ]; + const checkedDirectives = new Set([ + JAVA, JAVAC_OPTIONS, COMPILE_OPTIONS, DESCRIPTION, CDS, GAV, JAVAAGENT, MANIFEST, + JAVA_OPTIONS, RUNTIME_OPTIONS, NATIVE_OPTIONS, KOTLIN, GROOVY, MAIN, MODULE, PREVIEW, QUARKUS_DEP + ]); const lines = document.getText().split(/\r?\n/); - for (let i = 0; i < lines.length && checkedDirectives.length > 0; i++) { - const line = lines[i]; - let found; - for(let j = 0; j < checkedDirectives.length; j++) { - const directive = checkedDirectives[j]; - if (directive.matches(line, true)) { - found = directive; - break; - } + + for (const line of lines) { + if (checkedDirectives.size === 0) { + break; } - if (found) { - this.directives.push(found); - const index = checkedDirectives.indexOf(found, 0); - if (index > -1) { - checkedDirectives.splice(index, 1); + for (const directive of checkedDirectives) { + const includeSpace = directive !== QUARKUS_DEP;//special case for Quarkus dependencies + if (directive.matches(line, includeSpace)) { + this.directives.push(directive); + checkedDirectives.delete(directive); + break; } } - } + } } } function getCompletion(directive: Directive, range?: Range, command?: Command): CompletionItem {