Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions packages/common/component/LifeCycles.vue
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ export default {
bindLifeCycles: {},
editorValue: '{}',
hasError: false,
linterWorker: null
linterWorker: null,
completionProvider: null
})

watchEffect(() => {
Expand Down Expand Up @@ -210,7 +211,7 @@ export default {
return
}
// Lowcode API 提示
initCompletion(editorRef.value.getMonaco())
state.completionProvider = initCompletion(editorRef.value.getMonaco(), editorRef.value.getEditor()?.getModel())

// 初始化 ESLint worker
state.linterWorker = initLinter(editor, editorRef.value.getMonaco(), state)
Expand All @@ -226,6 +227,9 @@ export default {
}

onBeforeUnmount(() => {
state.completionProvider.forEach((provider) => {
provider.dispose()
})
// 终止 ESLint worker
state.linterWorker?.terminate?.()
})
Expand Down
14 changes: 10 additions & 4 deletions packages/common/js/completion.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ const getWords = (model, position) => {
const lastWord = model.getWordUntilPosition(lastPosition).word
? model.getWordUntilPosition(lastPosition)
: getCurrentChar(model, lastPosition)
if (!/[\w\.]/.test(lastWord.word)) break
if (!/[\w.]/.test(lastWord.word)) break
words.push(lastWord)
lastPosition.column = lastWord.startColumn
}
Expand All @@ -172,9 +172,14 @@ const getRange = (position, words) => ({
endColumn: words[words.length - 1].endColumn
})

export const initCompletion = (monacoInstance) => {
export const initCompletion = (monacoInstance, editorModel, conditionFn) => {
const completionItemProvider = {
provideCompletionItems(model, position, _context, _token) {
if (editorModel && model.id !== editorModel.id) {
return {
suggestions: []
}
}
const words = getWords(model, position)
const wordContent = words.map((item) => item.word).join('')
const range = getRange(position, words)
Expand All @@ -185,9 +190,10 @@ export const initCompletion = (monacoInstance) => {
const snippetSuggestions = getSnippetsSuggestions(monacoInstance, range, wordContent)
// 用户变量数据提示 e.g. this.dataSourceMap.xxx.load()
const userSuggestions = getUserSuggestions(monacoInstance, range, wordContent)

return {
suggestions: [...apiSuggestions, ...snippetSuggestions, ...userSuggestions]
suggestions: [...apiSuggestions, ...snippetSuggestions, ...userSuggestions].filter((item) =>
conditionFn ? conditionFn(item) : true
)
}
},
triggerCharacters: ['.']
Expand Down
36 changes: 33 additions & 3 deletions packages/plugins/data/src/CreateVariable.vue
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@
<li class="ml20">示例2: function fnName() {}</li>
<li class="ml20">示例3: { getValue: () => {} }</li>
</ul>
<div class="create-content-foot">
<div class="create-content-tip">
注意:使用JS表达式定义state变量的时候无法调用state其他变量定义,<br />另由于JS函数定义在变量之后,也无法调用JS面板定义的函数
</div>
</div>
</div>
</div>
<template #reference>
Expand Down Expand Up @@ -112,10 +117,11 @@
</template>

<script>
import { reactive, ref, computed, watch } from 'vue'
import { reactive, ref, computed, watch, onBeforeUnmount } from 'vue'
import { Popover, Form, FormItem, Input, ButtonGroup } from '@opentiny/vue'
import { MonacoEditor } from '@opentiny/tiny-engine-common'
import { verifyJsVarName } from '@opentiny/tiny-engine-common/js/verification'
import { initCompletion } from '@opentiny/tiny-engine-common/js/completion'
import * as Monaco from 'monaco-editor'
import { validateMonacoEditorData } from './js/common'
import EditorI18nTool from './EditorI18nTool.vue'
Expand Down Expand Up @@ -188,8 +194,14 @@ export default {
lineNumbers: true,
// 禁用滚动条边边一直显示的边框
overviewRulerBorder: false,
renderLineHighlightOnlyWhenFocus: true
}
renderLineHighlightOnlyWhenFocus: true,
quickSuggestions: false, // 快速提示禁用,避免调用其他模块提供的函数,因为变量是最先初始化
suggest: {
showFields: false,
showFunctions: false
}
},
completionProvider: null
})

const changeLanguage = (language) => {
Expand Down Expand Up @@ -314,8 +326,21 @@ export default {
noSyntaxValidation: true,
noSemanticValidation: true
})
if (variableEditor.value) {
state.completionProvider = initCompletion(
variableEditor.value.editor.getMonaco(),
variableEditor.value.editor.getEditor()?.getModel(),
(item) => item.label !== 'this.state' && !item.label.startsWith('this.state.')
)
}
}

onBeforeUnmount(() => {
state.completionProvider?.forEach((provider) => {
provider.dispose()
})
})

const insertContent = (insertText = '') => {
const monacoEditor = getEditor().editor.getEditor()
const selection = monacoEditor.getSelection()
Expand Down Expand Up @@ -445,6 +470,11 @@ export default {
margin-top: 8px;
}
}
.create-content-foot {
margin-top: 4px;
font-size: 14px;
line-height: 22px;
}
}

.create-content-description {
Expand Down
5 changes: 4 additions & 1 deletion packages/plugins/script/src/Main.vue
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,16 @@ export default {
}

// Lowcode API 提示
initCompletion(monaco.value.getMonaco())
state.completionProvider = initCompletion(monaco.value.getMonaco(), monaco.value.getEditor()?.getModel())

// 初始化 ESLint worker
state.linterWorker = initLinter(editor, monaco.value.getMonaco(), state)
}

onBeforeUnmount(() => {
state.completionProvider?.forEach((provider) => {
provider.dispose()
})
// 终止 ESLint worker
state.linterWorker?.terminate?.()
})
Expand Down
3 changes: 2 additions & 1 deletion packages/plugins/script/src/js/method.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ const state = reactive({
script: '',
isChanged: false,
hasError: false,
editorSelection: null
editorSelection: null,
completionProvider: null
})

const monaco = ref(null)
Expand Down