From 401bc63e4d636062a4afd39d8e79c47144aa2eed Mon Sep 17 00:00:00 2001 From: Zoltan Szabo <63643463+zoltanszabo-bitrise@users.noreply.github.com> Date: Thu, 12 Dec 2024 08:34:06 +0100 Subject: [PATCH] feat: improve completion provider (#1364) --- .../components/StepCodeEditor.tsx | 2 -- .../hooks/useMonacoCompletionProvider.ts | 24 +++++++++++++++---- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/source/javascripts/components/unified-editor/StepConfigDrawer/components/StepCodeEditor.tsx b/source/javascripts/components/unified-editor/StepConfigDrawer/components/StepCodeEditor.tsx index ef1cd4320..8c308be1f 100644 --- a/source/javascripts/components/unified-editor/StepConfigDrawer/components/StepCodeEditor.tsx +++ b/source/javascripts/components/unified-editor/StepConfigDrawer/components/StepCodeEditor.tsx @@ -13,8 +13,6 @@ const EDITOR_OPTIONS = { contextmenu: false, minimap: { enabled: false }, padding: { top: 16, bottom: 16 }, - suggestOnTriggerCharacters: true, - quickSuggestions: false, }; type Props = { diff --git a/source/javascripts/hooks/useMonacoCompletionProvider.ts b/source/javascripts/hooks/useMonacoCompletionProvider.ts index 23b888cbd..bd438ef16 100644 --- a/source/javascripts/hooks/useMonacoCompletionProvider.ts +++ b/source/javascripts/hooks/useMonacoCompletionProvider.ts @@ -15,12 +15,26 @@ const useEnvVarsAndSecretsCompletionProvider = ({ monaco, language }: Props) => () => ({ triggerCharacters: ['$'], provideCompletionItems: (model, position) => { - const word = model.getWordUntilPosition(position); + const wordUntilPosition = model.getWordUntilPosition(position); + const { startColumn, endColumn } = wordUntilPosition; + + const { lineNumber } = position; + const lineContent = model.getLineContent(lineNumber); + // NOTE Needs to be -2 because column is 1-based, and chars are 0-based + // -1 would get us the char at the cursor + const prefixPos = startColumn - 2; + const prefixChar = lineContent.charAt(prefixPos); + + // Word doesn't have $ prefix, so it's not an env var or secret + if (prefixChar !== '$') { + return { suggestions: [] }; + } + const range = { - startLineNumber: position.lineNumber, - startColumn: word.startColumn, - endLineNumber: position.lineNumber, - endColumn: word.endColumn, + startLineNumber: lineNumber, + startColumn, + endLineNumber: lineNumber, + endColumn, }; const suggestions: languages.CompletionItem[] = items.map((item) => ({