Browse Source

代码提示优化

mxd 3 years ago
parent
commit
5207dc9c35

+ 5 - 3
magic-editor/src/console/src/components/editor/magic-script-editor.vue

@@ -80,6 +80,7 @@ import {TokenStream} from '@/scripts/parsing/index.js'
 import RequestParameter from '@/scripts/editor/request-parameter.js';
 import { CommandsRegistry } from 'monaco-editor/esm/vs/platform/commands/common/commands'
 import { KeybindingsRegistry } from 'monaco-editor/esm/vs/platform/keybinding/common/keybindingsRegistry.js'
+import { ContextKeyExpr } from 'monaco-editor/esm/vs/platform/contextkey/common/contextkey.js'
 
 export default {
   name: 'MagicScriptEditor',
@@ -140,21 +141,22 @@ export default {
         '!findWidgetVisible && !inreferenceSearchEditor && !editorHasSelection'
     )
     const updateKeys = [
-        ['editor.action.triggerSuggest', monaco.KeyMod.Alt | monaco.KeyCode.US_SLASH],
         ['editor.action.triggerParameterHints', monaco.KeyMod.Alt | monaco.KeyCode.US_SLASH],
-        ['toggleSuggestionDetails', monaco.KeyMod.Alt | monaco.KeyCode.US_SLASH],
+        ['editor.action.triggerSuggest', monaco.KeyMod.Alt | monaco.KeyCode.US_SLASH],
+        ['toggleSuggestionDetails', monaco.KeyMod.Alt | monaco.KeyCode.US_SLASH, ContextKeyExpr.deserialize('suggestWidgetVisible && textInputFocus')],
         ['editor.action.formatDocument', monaco.KeyMod.CtrlCmd | monaco.KeyMod.Alt | monaco.KeyCode.KEY_L],
         ['editor.action.marker.nextInFiles', monaco.KeyMod.CtrlCmd | monaco.KeyCode.F8]
     ]
     updateKeys.forEach(item => {
       let action = item[0]
       const { handler, when } = CommandsRegistry.getCommand(action) ?? {}
+      console.log(action, when, handler)
       if (handler) {
         let index = KeybindingsRegistry._coreKeybindings.findIndex(it => it.command === action);
         if(index > 0){
           KeybindingsRegistry._coreKeybindings.splice(index, 1)
         }
-        this.editor._standaloneKeybindingService.addDynamicKeybinding(action, item[1], handler, when)
+        this.editor._standaloneKeybindingService.addDynamicKeybinding(action, item[1], handler, when || item[2])
       }
     })
     KeybindingsRegistry._cachedMergedKeybindings = null;

+ 36 - 28
magic-editor/src/console/src/scripts/editor/hover.js

@@ -41,6 +41,33 @@ const generateMethodDocument = (prefix,method, contents) => {
     })
     contents.push({value: `返回类型:\`${method.returnType}\``})
 }
+
+const generateFunctionCall = (methodName, env, contents)=>{
+    let functions = JavaClass.findFunction().filter(method => method.name === methodName);
+    if (functions.length > 0) {
+        generateMethodDocument('', functions[0], contents);
+    } else {
+        let value = env[methodName];
+        if (value && value.indexOf('@') === 0) {
+            let functionName = value.substring(1);
+            let func = JavaClass.getOnlineFunction(functionName);
+            if (func) {
+                let parameters = Array.isArray(func.parameter) ? func.parameter : JSON.parse(func.parameter || '[]');
+                parameters.forEach(it => it.comment = it.description);
+                generateMethodDocument('', {
+                    fullName: methodName + " " + func.name,
+                    comment: func.description || '',
+                    parameters,
+                    returnType: func.returnType
+                }, contents);
+            }
+
+        } else {
+            contents.push({value: `访问变量:${methodName}`})
+            contents.push({value: `类型:${value || 'unknow'}`})
+        }
+    }
+}
 const HoverProvider = {
     provideHover: async (model, position) => {
         let value = model.getValue()
@@ -63,8 +90,8 @@ const HoverProvider = {
                 let line = best.getSpan().getLine();
                 if (best instanceof VarDefine) {
                     let value = env[best.getVarName()];
-                    contents.push({value: `变量:${best.getVarName()}`})
-                    contents.push({value: `类型:${value}`})
+                    contents.push({value: `定义变量:${best.getVarName()}`})
+                    contents.push({value: `变量类型:${value}`})
                 } else if (best instanceof ClassConverter) {
                     if(best.convert === 'json'){
                         contents.push({value: '强制转换为`JSON`类型'})
@@ -78,8 +105,12 @@ const HoverProvider = {
                     }
                 } else if (best instanceof VariableAccess) {
                     let value = env[best.getVariable()];
-                    contents.push({value: `访问变量:${best.getVariable()}`})
-                    contents.push({value: `类型:${value || 'unknow'}`})
+                    if(value){
+                        contents.push({value: `访问变量:${best.getVariable()}`})
+                        contents.push({value: `变量类型:${value || 'unknow'}`})
+                    }else{
+                        generateFunctionCall(best.getVariable(), env, contents)
+                    }
                 } else if (best instanceof MemberAccess) {
                     let javaType = await best.getTarget().getJavaType(env);
                     let clazz = await JavaClass.loadClass(javaType);
@@ -97,30 +128,7 @@ const HoverProvider = {
                     line = best.member.getLine();
                 } else if (best instanceof FunctionCall) {
                     let target = best.target;
-                    let functions = JavaClass.findFunction().filter(method => method.name === target.variable);
-                    if (functions.length > 0) {
-                        generateMethodDocument('', functions[0], contents);
-                    } else {
-                        let value = env[target.variable];
-                        if (value && value.indexOf('@') === 0) {
-                            let functionName = value.substring(1);
-                            let func = JavaClass.getOnlineFunction(functionName);
-                            if (func) {
-                                let parameters = Array.isArray(func.parameter) ? func.parameter : JSON.parse(func.parameter || '[]');
-                                parameters.forEach(it => it.comment = it.description);
-                                generateMethodDocument('', {
-                                    fullName: target.variable + " " + func.name,
-                                    comment: func.description || '',
-                                    parameters,
-                                    returnType: func.returnType
-                                }, contents);
-                            }
-
-                        } else {
-                            contents.push({value: `访问变量:${target.variable}`})
-                            contents.push({value: `类型:${value || 'unknow'}`})
-                        }
-                    }
+                    generateFunctionCall(target.variable, env, contents)
                 } else if (best instanceof MapOrArrayAccess) {
                     contents.push({value: `访问Map或数组`})
                 } else if (best instanceof LinqSelect) {

+ 8 - 1
magic-editor/src/console/src/scripts/parsing/ast.js

@@ -10,10 +10,17 @@ class Node {
         return this.span;
     }
 
-    async getJavaType() {
+    async getJavaType(env) {
+        await this.getExpressionsJavaType(env);
         return 'java.lang.Object';
     }
 
+    async getExpressionsJavaType(env){
+        for (const expr of this.expressions().filter(it => it)) {
+            await expr.getJavaType(env);
+        }
+    }
+
     expressions() {
         return []
     }