Browse Source

feat: support `globalVars` in hints

tophf 5 months ago
parent
commit
067769f189

+ 1 - 1
src/common/ui/code-autocomplete.js

@@ -3,7 +3,7 @@ import { defaultsEditor, kAutocompleteOnTyping as ID } from '@/common/options-de
 
 
 const OPTIONS = 'options';
 const OPTIONS = 'options';
 const STATE = 'state';
 const STATE = 'state';
-const HINT_OPTIONS = 'hintOptions';
+export const HINT_OPTIONS = 'hintOptions';
 const COMPLETE_SINGLE = 'completeSingle';
 const COMPLETE_SINGLE = 'completeSingle';
 const PICKED = 'picked';
 const PICKED = 'picked';
 const TIMER = 'timer';
 const TIMER = 'timer';

+ 4 - 1
src/common/ui/code-js-mixed-mode.js

@@ -34,7 +34,10 @@ CodeMirror.defineMode('javascript-mixed', (config) => {
   const cmStartState = CodeMirror.startState;
   const cmStartState = CodeMirror.startState;
   const cmPass = CodeMirror.Pass;
   const cmPass = CodeMirror.Pass;
 
 
-  const jsMode = CodeMirror.getMode(config, { name: 'javascript' });
+  const jsMode = CodeMirror.getMode(config, {
+    name: 'javascript',
+    globalVars: config.globalVars,
+  });
   const jsTokenQuasi = (() => {
   const jsTokenQuasi = (() => {
     // create a new stream of a non-ending (1st line of a multiline)
     // create a new stream of a non-ending (1st line of a multiline)
     // string template to obtain tokenQuasi tokenizer
     // string template to obtain tokenQuasi tokenizer

+ 5 - 1
src/common/ui/code.vue

@@ -109,6 +109,7 @@ CodeMirror.registerHelper('hint', 'autoHintWithFallback', (cm, ...args) => {
 import { nextTick, onBeforeUnmount, onMounted, reactive, ref, watch, watchEffect } from 'vue';
 import { nextTick, onBeforeUnmount, onMounted, reactive, ref, watch, watchEffect } from 'vue';
 import Tooltip from 'vueleton/lib/tooltip';
 import Tooltip from 'vueleton/lib/tooltip';
 import ToggleButton from '@/common/ui/toggle-button';
 import ToggleButton from '@/common/ui/toggle-button';
+import { HINT_OPTIONS } from './code-autocomplete';
 
 
 let cm;
 let cm;
 let maxDisplayLength;
 let maxDisplayLength;
@@ -494,9 +495,12 @@ onMounted(() => {
     ...userOpts,
     ...userOpts,
     ...theme && { theme },
     ...theme && { theme },
     ...internalOpts, // internal options passed via `props` have the highest priority
     ...internalOpts, // internal options passed via `props` have the highest priority
-    mode: props.mode || cmDefaults.mode,
+    mode: props.mode || userOpts.mode || cmDefaults.mode,
   };
   };
   const cmWrapper = $cmWrapper.value;
   const cmWrapper = $cmWrapper.value;
+  const ho = opts[HINT_OPTIONS];
+  const hoWord = ho?.word;
+  if (hoWord) try { ho.word = new RegExp(hoWord); } catch {/**/}
   cm = CodeMirror(cmWrapper, opts);
   cm = CodeMirror(cmWrapper, opts);
   initialize();
   initialize();
   onActive(true); // DANGER! Must precede expandKeyMap.
   onActive(true); // DANGER! Must precede expandKeyMap.