Просмотр исходного кода

feat: autocomplete GM API in editor

tophf 1 месяц назад
Родитель
Сommit
2bd3ab0051
2 измененных файлов с 48 добавлено и 3 удалено
  1. 31 0
      src/common/consts.js
  2. 17 3
      src/common/ui/code.vue

+ 31 - 0
src/common/consts.js

@@ -58,3 +58,34 @@ export const XHR_COOKIE_RE = /:\W+([-\w]+)/; // extracts ://id in Chrome, ://{id
 /** @type {(str: string, opts?: {}) => Uint8Array} */
 export const U8_fromBase64 = process.env.IS_INJECTED !== 'injected-web' && Uint8Array.fromBase64;
 export const UPLOAD = 'upload';
+export const GM_API_NAMES = [
+  'GM',
+  'GM_addElement',
+  'GM_addStyle',
+  'GM_addValueChangeListener',
+  'GM_deleteValue',
+  'GM_deleteValues',
+  'GM_download',
+  'GM_getResourceText',
+  'GM_getResourceURL',
+  'GM_getValue',
+  'GM_getValues',
+  'GM_info',
+  'GM_listValues',
+  'GM_log',
+  'GM_notification',
+  'GM_openInTab',
+  'GM_registerMenuCommand',
+  'GM_removeValueChangeListener',
+  'GM_setClipboard',
+  'GM_setValue',
+  'GM_setValues',
+  'GM_unregisterMenuCommand',
+  'GM_xmlhttpRequest',
+  'unsafeWindow',
+];
+export const GM4_ALIAS = {
+  __proto__: null,
+  getResourceURL: 'getResourceUrl',
+  xmlhttpRequest: 'xmlHttpRequest',
+};

+ 17 - 3
src/common/ui/code.vue

@@ -74,6 +74,7 @@ import 'codemirror/addon/hint/javascript-hint';
 import 'codemirror/addon/hint/anyword-hint';
 import CodeMirror from 'codemirror';
 import { debounce, getUniqId, i18n, sendCmdDirectly } from '@/common';
+import { GM_API_NAMES, GM4_ALIAS } from '@/common/consts';
 import { deepEqual, forEachEntry, objectPick } from '@/common/object';
 import hookSetting from '@/common/hook-setting';
 import options from '@/common/options';
@@ -94,14 +95,27 @@ const cmOrigCommands = Object.assign({}, cmCommands);
 const { insertTab, insertSoftTab } = cmCommands;
 /** Using space prefix to show the command at the top of Help list */
 const Esc = ' back / cancel / close / singleSelection';
-
+const GM4_API_NAMES = GM_API_NAMES.map(s => GM4_ALIAS[s = s.slice(3)] || s);
 Object.assign(CodeMirror.keyMap.sublime, {
   'Shift-Ctrl-/': 'commentSelection',
 });
 CodeMirror.registerHelper('hint', 'autoHintWithFallback', (cm, ...args) => {
-  const result = cm.getHelper(cm.getCursor(), 'hint')?.(cm, ...args);
+  let result = cm.getHelper(cm.getCursor(), 'hint')?.(cm, ...args);
   // fallback to anyword if default returns nothing (or no default)
-  return result?.list.length ? result : CodeMirror.hint.anyword(cm, ...args);
+  result = result?.list.length ? result : CodeMirror.hint.anyword(cm, ...args);
+  if (result) {
+    const { from: { ch, line }, list } = result;
+    const prev = cm.getRange({ch: ch < 3 ? ch : ch - 3, line}, result.to);
+    const prefix = ch < 3 ? prev : prev.slice(3);
+    const v4 = ch >= 3 && prev.startsWith('GM.');
+    if (v4) list.length = 0;
+    for (const s of v4 ? GM4_API_NAMES : GM_API_NAMES) {
+      if (s.startsWith(prefix) && (v4 || !list.includes(s))) {
+        list.push(s);
+      }
+    }
+  }
+  return result;
 });
 </script>