Skip to content

Commit 454a4bf

Browse files
committed
LuaPredefinedScope
1 parent 71af70a commit 454a4bf

File tree

7 files changed

+65
-69
lines changed

7 files changed

+65
-69
lines changed

src/main/java/com/tang/intellij/lua/codeInsight/inspection/DuplicateClassDeclaration.java

Lines changed: 0 additions & 54 deletions
This file was deleted.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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.codeInsight.inspection
18+
19+
import com.intellij.codeInspection.LocalInspectionTool
20+
import com.intellij.codeInspection.LocalInspectionToolSession
21+
import com.intellij.codeInspection.ProblemHighlightType
22+
import com.intellij.codeInspection.ProblemsHolder
23+
import com.intellij.psi.PsiElementVisitor
24+
import com.tang.intellij.lua.comment.psi.LuaDocClassDef
25+
import com.tang.intellij.lua.comment.psi.LuaDocVisitor
26+
import com.tang.intellij.lua.search.LuaPredefinedScope
27+
import com.tang.intellij.lua.stubs.index.LuaClassIndex
28+
29+
/**
30+
* 重复定义class
31+
* Created by TangZX on 2016/12/16.
32+
*/
33+
class DuplicateClassDeclaration : LocalInspectionTool() {
34+
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean, session: LocalInspectionToolSession): PsiElementVisitor {
35+
return object : LuaDocVisitor() {
36+
override fun visitClassDef(o: LuaDocClassDef) {
37+
val identifier = o.nameIdentifier
38+
val classDefs = LuaClassIndex.getInstance().get(identifier.text, o.project, LuaPredefinedScope(o.project))
39+
if (classDefs.size > 1) {
40+
holder.registerProblem(identifier, "Duplicate class", ProblemHighlightType.ERROR)
41+
}
42+
}
43+
}
44+
}
45+
}

src/main/java/com/tang/intellij/lua/codeInsight/intention/CreateMethodIntention.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@
2424
import com.intellij.openapi.project.Project;
2525
import com.intellij.openapi.util.TextRange;
2626
import com.intellij.psi.PsiFile;
27-
import com.intellij.psi.search.ProjectAndLibrariesScope;
2827
import com.intellij.psi.util.PsiTreeUtil;
2928
import com.intellij.util.IncorrectOperationException;
3029
import com.tang.intellij.lua.lang.type.LuaType;
3130
import com.tang.intellij.lua.lang.type.LuaTypeSet;
3231
import com.tang.intellij.lua.psi.*;
32+
import com.tang.intellij.lua.search.LuaPredefinedScope;
3333
import com.tang.intellij.lua.search.SearchContext;
3434
import com.tang.intellij.lua.stubs.index.LuaClassMethodIndex;
3535
import org.jetbrains.annotations.Nls;
@@ -74,7 +74,7 @@ public void invoke(@NotNull Project project, Editor editor, PsiFile psiFile) thr
7474
if (expr instanceof LuaIndexExpr) {
7575
LuaIndexExpr indexExpr = (LuaIndexExpr) expr;
7676
LuaTypeSet typeSet = indexExpr.guessPrefixType(new SearchContext(project));
77-
if (typeSet.isEmpty()) return;
77+
if (typeSet == null || typeSet.isEmpty()) return;
7878

7979
InsertPosition position = calcInsertPosition(typeSet.getPerfect(), project);
8080
if (position != null) {
@@ -97,7 +97,9 @@ private static class InsertPosition {
9797

9898
@Nullable
9999
private InsertPosition calcInsertPosition(LuaType perfect, Project project) {
100-
Collection<LuaClassMethodDef> methods = LuaClassMethodIndex.getInstance().get(perfect.getClassName(), project, new ProjectAndLibrariesScope(project));
100+
Collection<LuaClassMethodDef> methods = LuaClassMethodIndex.getInstance().get(perfect.getClassName(),
101+
project,
102+
new LuaPredefinedScope(project));
101103
if (!methods.isEmpty()) {
102104
LuaClassMethodDef methodDef = methods.iterator().next();
103105
LuaExpr expr = methodDef.getClassMethodName().getExpr();

src/main/java/com/tang/intellij/lua/comment/reference/LuaClassNameReference.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ package com.tang.intellij.lua.comment.reference
1919
import com.intellij.openapi.util.TextRange
2020
import com.intellij.psi.PsiElement
2121
import com.intellij.psi.PsiReferenceBase
22-
import com.intellij.psi.search.ProjectAndLibrariesScope
2322
import com.tang.intellij.lua.comment.psi.LuaDocClassNameRef
23+
import com.tang.intellij.lua.search.LuaPredefinedScope
2424
import com.tang.intellij.lua.stubs.index.LuaClassIndex
2525

2626
/**
@@ -37,7 +37,7 @@ class LuaClassNameReference(element: LuaDocClassNameRef) : PsiReferenceBase<LuaD
3737

3838
override fun resolve(): PsiElement? {
3939
val name = myElement.text
40-
val defs = LuaClassIndex.getInstance().get(name, myElement.project, ProjectAndLibrariesScope(myElement.project))
40+
val defs = LuaClassIndex.getInstance().get(name, myElement.project, LuaPredefinedScope(myElement.project))
4141
if (defs.isNotEmpty()) {
4242
return defs.firstOrNull()
4343
}

src/main/java/com/tang/intellij/lua/editor/completion/OverrideCompletionProvider.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@
2424
import com.intellij.codeInsight.template.TemplateManager;
2525
import com.intellij.openapi.project.Project;
2626
import com.intellij.psi.PsiElement;
27-
import com.intellij.psi.search.ProjectAndLibrariesScope;
2827
import com.intellij.psi.util.PsiTreeUtil;
2928
import com.intellij.util.ProcessingContext;
3029
import com.tang.intellij.lua.lang.LuaIcons;
3130
import com.tang.intellij.lua.lang.type.LuaType;
3231
import com.tang.intellij.lua.psi.LuaClassMethodDef;
3332
import com.tang.intellij.lua.psi.LuaFuncBodyOwner;
3433
import com.tang.intellij.lua.psi.LuaParamInfo;
34+
import com.tang.intellij.lua.search.LuaPredefinedScope;
3535
import com.tang.intellij.lua.search.SearchContext;
3636
import com.tang.intellij.lua.stubs.index.LuaClassMethodIndex;
3737
import org.jetbrains.annotations.NotNull;
@@ -62,7 +62,7 @@ private void addOverrideMethod(@NotNull CompletionParameters completionParameter
6262
Project project = completionParameters.getOriginalFile().getProject();
6363
SearchContext context = new SearchContext(project);
6464
String clazzName = sup.getClassName();
65-
Collection<LuaClassMethodDef> list = LuaClassMethodIndex.getInstance().get(clazzName, project, new ProjectAndLibrariesScope(project));
65+
Collection<LuaClassMethodDef> list = LuaClassMethodIndex.getInstance().get(clazzName, project, new LuaPredefinedScope(project));
6666
for (LuaClassMethodDef def : list) {
6767
String methodName = def.getName();
6868
if (methodName != null) {

src/main/java/com/tang/intellij/lua/lang/type/LuaType.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import com.intellij.openapi.project.Project;
2020
import com.intellij.psi.PsiElement;
21-
import com.intellij.psi.search.ProjectAndLibrariesScope;
2221
import com.intellij.psi.stubs.StubInputStream;
2322
import com.intellij.psi.stubs.StubOutputStream;
2423
import com.intellij.util.io.StringRef;
@@ -27,6 +26,7 @@
2726
import com.tang.intellij.lua.psi.LuaClassMethodDef;
2827
import com.tang.intellij.lua.psi.LuaNameExpr;
2928
import com.tang.intellij.lua.psi.LuaPsiResolveUtil;
29+
import com.tang.intellij.lua.search.LuaPredefinedScope;
3030
import com.tang.intellij.lua.search.SearchContext;
3131
import com.tang.intellij.lua.stubs.index.LuaClassFieldIndex;
3232
import com.tang.intellij.lua.stubs.index.LuaClassIndex;
@@ -146,12 +146,11 @@ public void processFields(@NotNull SearchContext context,
146146
if (clazzName == null)
147147
return;
148148
Project project = context.getProject();
149-
assert project != null;
150149

151150
LuaClassFieldIndex fieldIndex = LuaClassFieldIndex.getInstance();
152-
Collection<LuaClassField> list = fieldIndex.get(clazzName, project, new ProjectAndLibrariesScope(project));
151+
Collection<LuaClassField> list = fieldIndex.get(clazzName, project, new LuaPredefinedScope(project));
153152
if (aliasName != null) {
154-
Collection<LuaClassField> classFields = fieldIndex.get(aliasName, project, new ProjectAndLibrariesScope(project));
153+
Collection<LuaClassField> classFields = fieldIndex.get(aliasName, project, new LuaPredefinedScope(project));
155154
list.addAll(classFields);
156155
}
157156

@@ -171,12 +170,11 @@ public void processMethods(@NotNull SearchContext context,
171170
if (clazzName == null)
172171
return;
173172
Project project = context.getProject();
174-
assert project != null;
175173

176174
LuaClassMethodIndex methodIndex = LuaClassMethodIndex.getInstance();
177-
Collection<LuaClassMethodDef> list = methodIndex.get(clazzName, project, new ProjectAndLibrariesScope(project));
175+
Collection<LuaClassMethodDef> list = methodIndex.get(clazzName, project, new LuaPredefinedScope(project));
178176
if (aliasName != null) {
179-
list.addAll(methodIndex.get(aliasName, project, new ProjectAndLibrariesScope(project)));
177+
list.addAll(methodIndex.get(aliasName, project, new LuaPredefinedScope(project)));
180178
}
181179
for (LuaClassMethodDef def : list) {
182180
String methodName = def.getName();

src/main/java/com/tang/intellij/lua/search/SearchContext.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class SearchContext(val project: Project) {
5353
if (isDumb) {
5454
scope = GlobalSearchScope.EMPTY_SCOPE
5555
} else {
56-
scope = AdditionalIndexedRootsScope(ProjectAndLibrariesScope(project), LuaPredefinedLibraryProvider::class.java)
56+
scope = LuaPredefinedScope(project)
5757
}
5858
}
5959
return scope!!
@@ -62,3 +62,8 @@ class SearchContext(val project: Project) {
6262
val isDumb: Boolean
6363
get() = DumbService.isDumb(project) || currentStubFile != null
6464
}
65+
66+
class LuaPredefinedScope : AdditionalIndexedRootsScope {
67+
constructor(project: Project) : this(ProjectAndLibrariesScope(project))
68+
constructor(base: GlobalSearchScope) : super(base, LuaPredefinedLibraryProvider::class.java)
69+
}

0 commit comments

Comments
 (0)