1+ /*
2+ * Copyright (c) 2017. tangzx([email protected] ) 3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * http://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ */
16+
17+ package com.tang.intellij.lua.editor.completion
18+
19+ import com.intellij.codeInsight.completion.PrioritizedLookupElement
20+ import com.intellij.codeInsight.lookup.LookupElement
21+ import com.intellij.codeInsight.lookup.LookupElementBuilder
22+ import com.intellij.psi.util.PsiTreeUtil
23+ import com.intellij.util.Processor
24+ import com.tang.intellij.lua.lang.LuaIcons
25+ import com.tang.intellij.lua.psi.LuaClassField
26+ import com.tang.intellij.lua.psi.LuaClassMember
27+ import com.tang.intellij.lua.psi.LuaTableExpr
28+ import com.tang.intellij.lua.psi.shouldBe
29+ import com.tang.intellij.lua.search.SearchContext
30+ import com.tang.intellij.lua.ty.ITy
31+
32+ class TableCompletionProvider : ClassMemberCompletionProvider () {
33+
34+ companion object {
35+ private val metaMethodNames = mapOf (
36+ " __add" to " a + b" ,
37+ " __sub" to " a - b" ,
38+ " __mul" to " a * b" ,
39+ " __div" to " a / b" ,
40+ " __mod" to " a % b" ,
41+ " __pow" to " a ^ b" ,
42+ " __unm" to " -a" ,
43+ " __concat" to " a .. b" ,
44+ " __len" to " #a" ,
45+ " __eq" to " a == a" ,
46+ " __lt" to " a < b" ,
47+ " __le" to " a <= b" ,
48+ " __index" to " Meta method" ,
49+ " __newindex" to " Meta method" ,
50+ " __call" to " Meta method" ,
51+ " __tostring" to " Meta method" ,
52+ " __metatable" to " Meta method"
53+ )
54+ }
55+ override fun addCompletions (session : CompletionSession ) {
56+ val completionParameters = session.parameters
57+ val completionResultSet = session.resultSet
58+ metaMethodNames.forEach {
59+ val b = LookupElementBuilder .create(it.key)
60+ // .withTypeText(it.value)
61+ .withIcon(LuaIcons .META_METHOD )
62+ completionResultSet.addElement(b)
63+ }
64+
65+ val table = PsiTreeUtil .getParentOfType(completionParameters.position, LuaTableExpr ::class .java)
66+ if (table != null ) {
67+ val project = table.project
68+ val prefixMatcher = completionResultSet.prefixMatcher
69+ val ty = table.shouldBe(SearchContext .get(project))
70+ ty.eachTopClass(Processor { luaType ->
71+ val context = SearchContext .get(project)
72+ luaType.lazyInit(context)
73+ luaType.processMembers(context) { curType, member ->
74+ member.name?.let {
75+ if (prefixMatcher.prefixMatches(it)) {
76+ val className = curType.displayName
77+ if (member is LuaClassField ) {
78+ addField(completionResultSet, curType == = luaType, className, member, null , object : HandlerProcessor () {
79+ override fun process (element : LuaLookupElement , member : LuaClassMember , memberTy : ITy ? ): LookupElement {
80+ element.itemText = element.itemText + " = "
81+ element.lookupString = element.lookupString + " = "
82+
83+ return PrioritizedLookupElement .withPriority(element, 10.0 )
84+ }
85+ })
86+ }
87+ }
88+ }
89+ }
90+ true
91+ })
92+ }
93+ }
94+ }
0 commit comments