From a7e4f80c2b1647eb30db2d1d7139c7993f6b92ab Mon Sep 17 00:00:00 2001 From: Patrick Scheibe Date: Sun, 6 May 2018 11:10:18 +0200 Subject: [PATCH] Fix several issues The FullForm creator had a bug with blanks which is fixed Small fix in hash of the package index Fix for wrong completions that came from a different project --- build.gradle | 2 +- resources/META-INF/change-notes.html | 7 +- resources/META-INF/plugin.xml | 1 - .../providers/CommentCompletion.java | 58 ++---- .../providers/FileSymbolCompletion.java | 39 ++-- .../providers/ImportedSymbolCompletion.java | 55 ++--- .../editoractions/MathematicaCommenter.java | 29 +-- .../enter/CommentStarInsertEnterHandler.kt | 81 -------- .../formatter/MathematicaIndentProcessor.java | 50 +---- .../MathematicaPackageExportIndex.java | 35 ++-- .../packageexport/PackageExportSymbol.java | 35 ++-- .../mathematica/lang/psi/impl/SymbolImpl.java | 35 ++-- .../psi/util/MathematicaFullFormCreator.java | 189 ------------------ .../psi/util/MathematicaFullFormCreator.kt | 183 +++++++++++++++++ .../MathematicaGlobalSymbolResolver.java | 41 ++-- .../MathematicaReferenceContributor.java | 13 +- 16 files changed, 357 insertions(+), 496 deletions(-) delete mode 100644 src/de/halirutan/mathematica/codeinsight/editoractions/enter/CommentStarInsertEnterHandler.kt delete mode 100644 src/de/halirutan/mathematica/lang/psi/util/MathematicaFullFormCreator.java create mode 100644 src/de/halirutan/mathematica/lang/psi/util/MathematicaFullFormCreator.kt diff --git a/build.gradle b/build.gradle index e7f61b9d..1d1b1459 100644 --- a/build.gradle +++ b/build.gradle @@ -65,7 +65,7 @@ task wrapper(type: Wrapper) { // Information about the plugin // Plugin version number -version '3.0pre11' +version '3.0pre12' intellij { version = '2018.1.2' diff --git a/resources/META-INF/change-notes.html b/resources/META-INF/change-notes.html index 5d9717e8..c5c06191 100644 --- a/resources/META-INF/change-notes.html +++ b/resources/META-INF/change-notes.html @@ -2,8 +2,11 @@ New features and bug-fixes in version 3:
\ No newline at end of file diff --git a/resources/META-INF/plugin.xml b/resources/META-INF/plugin.xml index c278c671..63cdcc86 100644 --- a/resources/META-INF/plugin.xml +++ b/resources/META-INF/plugin.xml @@ -67,7 +67,6 @@ - diff --git a/src/de/halirutan/mathematica/codeinsight/completion/providers/CommentCompletion.java b/src/de/halirutan/mathematica/codeinsight/completion/providers/CommentCompletion.java index e04e65b8..29ec600c 100644 --- a/src/de/halirutan/mathematica/codeinsight/completion/providers/CommentCompletion.java +++ b/src/de/halirutan/mathematica/codeinsight/completion/providers/CommentCompletion.java @@ -1,24 +1,23 @@ /* - * Copyright (c) 2017 Patrick Scheibe + * Copyright (c) 2018 Patrick Scheibe * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. */ package de.halirutan.mathematica.codeinsight.completion.providers; @@ -30,7 +29,6 @@ import com.intellij.psi.PsiElement; import com.intellij.psi.PsiFile; import com.intellij.util.ProcessingContext; -import com.intellij.util.text.StringTokenizer; import de.halirutan.mathematica.lang.MathematicaLanguage; import de.halirutan.mathematica.lang.resolve.MathematicaGlobalResolveCache; import org.jetbrains.annotations.NotNull; @@ -82,12 +80,11 @@ protected void addCompletions(@NotNull CompletionParameters parameters, Processi .addElement(LookupElementBuilder.create(" :" + tag + ": ")); } } else { - final String prefix = findCommentPrefix(parameters); final PsiFile file = parameters.getOriginalFile(); final MathematicaGlobalResolveCache symbolCache = MathematicaGlobalResolveCache.getInstance(file.getProject()); final List cachedDefinitions = symbolCache.getCachedFileSymbolNames(file); for (String definition : cachedDefinitions) { - result.withPrefixMatcher(new PlainPrefixMatcher(prefix)).addElement(LookupElementBuilder.create(definition)); + result.addElement(LookupElementBuilder.create(definition)); } } } @@ -102,25 +99,4 @@ private boolean isEmptyComment(CompletionParameters parameters) { return false; } - private String findCommentPrefix(CompletionParameters parameters) { - final int posOffset = parameters.getOffset(); - final PsiElement commentElement = parameters.getPosition(); - - if (commentElement instanceof PsiComment) { - final int elementStart = commentElement.getTextOffset(); - final String commentText = commentElement.getText().substring(0, posOffset - elementStart); - if (commentText.length() == 0 || commentText.matches(".*[ \t\n\f]")) { - return ""; - } - - StringTokenizer tokenizer = new StringTokenizer(commentText); - String prefix = ""; - while (tokenizer.hasMoreElements()) { - prefix = tokenizer.nextToken(); - } - return prefix; - } - return ""; - } - } diff --git a/src/de/halirutan/mathematica/codeinsight/completion/providers/FileSymbolCompletion.java b/src/de/halirutan/mathematica/codeinsight/completion/providers/FileSymbolCompletion.java index 0992117f..9cd3f844 100644 --- a/src/de/halirutan/mathematica/codeinsight/completion/providers/FileSymbolCompletion.java +++ b/src/de/halirutan/mathematica/codeinsight/completion/providers/FileSymbolCompletion.java @@ -1,24 +1,23 @@ /* - * Copyright (c) 2017 Patrick Scheibe + * Copyright (c) 2018 Patrick Scheibe * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. */ package de.halirutan.mathematica.codeinsight.completion.providers; @@ -26,18 +25,18 @@ import com.intellij.codeInsight.completion.*; import com.intellij.codeInsight.completion.impl.CamelHumpMatcher; import com.intellij.codeInsight.lookup.LookupElementBuilder; -import com.intellij.patterns.PlatformPatterns; import com.intellij.patterns.PsiElementPattern.Capture; import com.intellij.psi.PsiElement; import com.intellij.psi.PsiFile; import com.intellij.util.ProcessingContext; +import de.halirutan.mathematica.lang.parsing.MathematicaElementTypes; import de.halirutan.mathematica.lang.psi.api.MathematicaPsiFile; -import de.halirutan.mathematica.lang.psi.api.Symbol; import de.halirutan.mathematica.lang.resolve.SymbolResolveResult; import org.jetbrains.annotations.NotNull; import java.util.HashSet; +import static com.intellij.patterns.PlatformPatterns.psiElement; import static de.halirutan.mathematica.codeinsight.completion.MathematicaCompletionContributor.GLOBAL_VARIABLE_PRIORITY; @@ -48,7 +47,7 @@ public class FileSymbolCompletion extends MathematicaCompletionProvider { @Override public void addTo(CompletionContributor contributor) { - final Capture symbolPattern = PlatformPatterns.psiElement().withParent(Symbol.class); + final Capture symbolPattern = psiElement().withElementType(MathematicaElementTypes.IDENTIFIER); contributor.extend(CompletionType.BASIC, symbolPattern, this); } diff --git a/src/de/halirutan/mathematica/codeinsight/completion/providers/ImportedSymbolCompletion.java b/src/de/halirutan/mathematica/codeinsight/completion/providers/ImportedSymbolCompletion.java index 893390ee..3a6a4dd0 100644 --- a/src/de/halirutan/mathematica/codeinsight/completion/providers/ImportedSymbolCompletion.java +++ b/src/de/halirutan/mathematica/codeinsight/completion/providers/ImportedSymbolCompletion.java @@ -1,24 +1,23 @@ /* - * Copyright (c) 2017 Patrick Scheibe + * Copyright (c) 2018 Patrick Scheibe * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. */ package de.halirutan.mathematica.codeinsight.completion.providers; @@ -27,21 +26,23 @@ import com.intellij.codeInsight.lookup.LookupElementBuilder; import com.intellij.openapi.module.Module; import com.intellij.openapi.module.ModuleUtilCore; +import com.intellij.openapi.progress.ProgressManager; import com.intellij.openapi.project.Project; -import com.intellij.patterns.PlatformPatterns; import com.intellij.patterns.PsiElementPattern.Capture; import com.intellij.psi.PsiElement; import com.intellij.psi.PsiFile; import com.intellij.psi.search.GlobalSearchScope; -import com.intellij.psi.search.ProjectScope; import com.intellij.util.ProcessingContext; import com.intellij.util.indexing.FileBasedIndex; +import com.intellij.util.indexing.IdFilter; import de.halirutan.mathematica.index.packageexport.MathematicaPackageExportIndex; +import de.halirutan.mathematica.lang.parsing.MathematicaElementTypes; import de.halirutan.mathematica.lang.psi.api.Symbol; import org.jetbrains.annotations.NotNull; import java.util.Objects; +import static com.intellij.patterns.PlatformPatterns.psiElement; import static de.halirutan.mathematica.codeinsight.completion.MathematicaCompletionContributor.IMPORT_VARIABLE_PRIORITY; @@ -52,7 +53,7 @@ public class ImportedSymbolCompletion extends MathematicaCompletionProvider { @Override public void addTo(CompletionContributor contributor) { - final Capture symbolPattern = PlatformPatterns.psiElement().withParent(Symbol.class); + final Capture symbolPattern = psiElement().withElementType(MathematicaElementTypes.IDENTIFIER); contributor.extend(CompletionType.BASIC, symbolPattern, this); } @@ -62,18 +63,24 @@ protected void addCompletions(@NotNull CompletionParameters parameters, Processi final Project project = callingSymbol.getProject(); String prefix = findCurrentText(parameters, parameters.getPosition()); + if (parameters.getInvocationCount() == 0 && prefix.isEmpty()) { + return; + } final PsiFile originalFile = parameters.getOriginalFile(); final Module module = ModuleUtilCore.findModuleForFile(originalFile.getVirtualFile(), project); if (module != null) { if (!parameters.isExtendedCompletion() || !(prefix.isEmpty() || Character.isDigit(prefix.charAt(0)))) { - final GlobalSearchScope moduleScope = GlobalSearchScope.moduleWithDependenciesAndLibrariesScope(module) - .union(ProjectScope.getLibrariesScope(project)); + + final GlobalSearchScope moduleScope = module.getModuleWithDependenciesAndLibrariesScope(true); final FileBasedIndex index = FileBasedIndex.getInstance(); + index.processAllKeys( MathematicaPackageExportIndex.INDEX_ID, key -> { - if (key.isExported() && !Objects.equals(key.getFileName(), originalFile.getName())) { + ProgressManager.checkCanceled(); + if (key.isExported() && !Objects.equals(key.getFileName(), originalFile.getName()) && + !index.getContainingFiles(MathematicaPackageExportIndex.INDEX_ID, key, moduleScope).isEmpty()) { result.addElement(PrioritizedLookupElement.withPriority( LookupElementBuilder.create(key.getSymbol()).withTypeText("(" + key.getFileName() + ")", true), IMPORT_VARIABLE_PRIORITY)); @@ -81,7 +88,7 @@ protected void addCompletions(@NotNull CompletionParameters parameters, Processi return true; }, moduleScope, - null + IdFilter.getProjectIdFilter(project, false) ); } } diff --git a/src/de/halirutan/mathematica/codeinsight/editoractions/MathematicaCommenter.java b/src/de/halirutan/mathematica/codeinsight/editoractions/MathematicaCommenter.java index 83c2fa21..4685e7bd 100644 --- a/src/de/halirutan/mathematica/codeinsight/editoractions/MathematicaCommenter.java +++ b/src/de/halirutan/mathematica/codeinsight/editoractions/MathematicaCommenter.java @@ -1,5 +1,6 @@ /* - * Copyright (c) 2017 Patrick Scheibe + * Copyright (c) 2018 Patrick Scheibe + * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights @@ -7,16 +8,16 @@ * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. */ package de.halirutan.mathematica.codeinsight.editoractions; @@ -42,23 +43,12 @@ public String getLineCommentPrefix() { return null; } - /** - * Returns the opening string for a comment. In Java and C this is /*, in Mathematica it is (* - * - * @return the comment opening string - */ @Nullable @Override public String getBlockCommentPrefix() { return "(*"; } - /** - * Returns the closing string for a block comment. In Java and C this is */ - * - * @return the block comment closing string - */ @Nullable @Override public String getBlockCommentSuffix() { @@ -68,13 +58,13 @@ public String getBlockCommentSuffix() { @Nullable @Override public String getCommentedBlockCommentPrefix() { - return "-"; + return null; } @Nullable @Override public String getCommentedBlockCommentSuffix() { - return "+"; + return null; } @Nullable @@ -115,6 +105,7 @@ public String getDocumentationCommentSuffix() { @Override public boolean isDocumentationComment(PsiComment element) { - return true; + return element.getTokenType().equals(MathematicaElementTypes.COMMENT); } + } diff --git a/src/de/halirutan/mathematica/codeinsight/editoractions/enter/CommentStarInsertEnterHandler.kt b/src/de/halirutan/mathematica/codeinsight/editoractions/enter/CommentStarInsertEnterHandler.kt deleted file mode 100644 index ed0ad111..00000000 --- a/src/de/halirutan/mathematica/codeinsight/editoractions/enter/CommentStarInsertEnterHandler.kt +++ /dev/null @@ -1,81 +0,0 @@ -/* - * Copyright (c) 2017. Patrick Scheibe - * - * Permission is hereby granted, free of charge, to any person - * obtaining a copy of this software and associated documentation - * files (the “Software”), to deal in the Software without - * restriction, including without limitation the rights to use, - * copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following - * conditions: - * - * The above copyright notice and this permission notice shall be - * included in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, - * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES - * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND - * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT - * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, - * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR - * OTHER DEALINGS IN THE SOFTWARE. - */ - -package de.halirutan.mathematica.codeinsight.editoractions.enter - -import com.intellij.codeInsight.editorActions.enter.EnterHandlerDelegate -import com.intellij.openapi.actionSystem.CommonDataKeys -import com.intellij.openapi.actionSystem.DataContext -import com.intellij.openapi.editor.Editor -import com.intellij.psi.PsiComment -import com.intellij.psi.PsiDocumentManager -import com.intellij.psi.PsiFile - -/** - * @Note Currently unused! - * Inserts a * in each new line of a multi-line comment - * @author patrick (04.12.17). - */ -class CommentStarInsertEnterHandler : MathematicaEnterHandler() { - override fun postProcessEnter(file: PsiFile, editor: Editor, dataContext: DataContext): EnterHandlerDelegate.Result { - skipWithResultQ(file, editor, dataContext)?.let { return it } - val caretModel = editor.caretModel - val offset = caretModel.offset - val project = dataContext.getData(CommonDataKeys.PROJECT) ?: return EnterHandlerDelegate.Result.Continue - val psiDocManager = PsiDocumentManager.getInstance(project) - - val comment = file.findElementAt(offset) ?: return EnterHandlerDelegate.Result.Continue - val commentRange = comment.textRange ?: return EnterHandlerDelegate.Result.Continue - - if (comment is PsiComment && offset >= commentRange.startOffset + 2 && offset <= commentRange.endOffset - 2) { - - val document = editor.document - - val lineNumber = document.getLineNumber(offset) - val elementStartLine = document.getLineNumber(comment.textOffset) - val elementEndLine = document.getLineNumber(comment.textOffset + comment.textLength) - - val insertString: String - val move: Int - if (lineNumber == elementStartLine + 1) { - insertString = " * " - move = 3 - } else { - insertString = "* " - move = 2 - } - - document.insertString(offset, insertString) - caretModel.moveToOffset(offset + move) - - if (lineNumber == elementEndLine) { - document.insertString(commentRange.endOffset + move - 2, "\n ") - } - psiDocManager.commitDocument(document) - return EnterHandlerDelegate.Result.DefaultSkipIndent - } - return EnterHandlerDelegate.Result.Continue - } -} \ No newline at end of file diff --git a/src/de/halirutan/mathematica/codeinsight/formatter/MathematicaIndentProcessor.java b/src/de/halirutan/mathematica/codeinsight/formatter/MathematicaIndentProcessor.java index 83f74eab..91d01ac6 100644 --- a/src/de/halirutan/mathematica/codeinsight/formatter/MathematicaIndentProcessor.java +++ b/src/de/halirutan/mathematica/codeinsight/formatter/MathematicaIndentProcessor.java @@ -1,5 +1,6 @@ /* - * Copyright (c) 2017 Patrick Scheibe + * Copyright (c) 2018 Patrick Scheibe + * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights @@ -7,16 +8,16 @@ * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. */ package de.halirutan.mathematica.codeinsight.formatter; @@ -33,19 +34,13 @@ */ class MathematicaIndentProcessor { - public static Indent getChildIndent(ASTNode node) { + static Indent getChildIndent(ASTNode node) { IElementType elementType = node.getElementType(); ASTNode parent = node.getTreeParent(); IElementType parentType = parent != null ? parent.getElementType() : null; - ASTNode grandfather = parent != null ? parent.getTreeParent() : null; - IElementType grandfatherType = grandfather != null ? grandfather.getElementType() : null; - ASTNode prevSibling = FormatterUtil.getPreviousNonWhitespaceSibling(node); - IElementType prevSiblingElementType = prevSibling != null ? prevSibling.getElementType() : null; - if (parent == null || - COMMENTS.contains(elementType) || - parentType == FILE) { + if (parent == null || parentType == FILE) { return Indent.getNoneIndent(); } @@ -87,29 +82,6 @@ public static Indent getChildIndent(ASTNode node) { } - /** - * Checks whether an ASTNode is in the function head. - * - * @param node - * the node to check - * @return true if node is in the function head or the opening bracket - */ - private static boolean isInFunctionHead(ASTNode node) { - final ASTNode treeParent = node.getTreeParent(); - if (treeParent == null) { - return false; - } - for (ASTNode child = treeParent.getFirstChildNode(); child != null; child = FormatterUtil.getNextNonWhitespaceSibling(child)) { - if (child == node) { - return true; - } - if (child.getElementType() == LEFT_BRACKET) { - return false; - } - } - return false; - } - /** * Checks whether an ASTNode is part of the function body, meaning one of the arguments of a function call or a comma * @@ -122,11 +94,11 @@ private static boolean isInFunctionBody(ASTNode node) { if (treeParent == null) { return false; } - boolean inBody = false; + boolean inBody = true; for (ASTNode child = treeParent.getFirstChildNode(); child != null; child = FormatterUtil.getNextNonWhitespaceSibling(child)) { - if (!inBody) { + if (inBody) { if (child.getElementType() == LEFT_BRACKET) { - inBody = true; + inBody = false; } continue; } diff --git a/src/de/halirutan/mathematica/index/packageexport/MathematicaPackageExportIndex.java b/src/de/halirutan/mathematica/index/packageexport/MathematicaPackageExportIndex.java index 8d4ee9d7..3e40a6a6 100644 --- a/src/de/halirutan/mathematica/index/packageexport/MathematicaPackageExportIndex.java +++ b/src/de/halirutan/mathematica/index/packageexport/MathematicaPackageExportIndex.java @@ -1,24 +1,23 @@ /* - * Copyright (c) 2017 Patrick Scheibe + * Copyright (c) 2018 Patrick Scheibe * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. */ package de.halirutan.mathematica.index.packageexport; @@ -43,7 +42,7 @@ public class MathematicaPackageExportIndex extends ScalarIndexExtension { public static final ID INDEX_ID = ID.create("Mathematica.fileExports"); - private static final int BASE_VERSION = 12; + private static final int BASE_VERSION = 13; private static final LanguageFileType MATHEMATICA_FILE_TYPE = MathematicaLanguage.INSTANCE.getAssociatedFileType(); private static final Set IGNORED_FILES = new HashSet<>(); diff --git a/src/de/halirutan/mathematica/index/packageexport/PackageExportSymbol.java b/src/de/halirutan/mathematica/index/packageexport/PackageExportSymbol.java index 4a4b8622..a0054370 100644 --- a/src/de/halirutan/mathematica/index/packageexport/PackageExportSymbol.java +++ b/src/de/halirutan/mathematica/index/packageexport/PackageExportSymbol.java @@ -1,24 +1,23 @@ /* - * Copyright (c) 2017 Patrick Scheibe + * Copyright (c) 2018 Patrick Scheibe * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. */ package de.halirutan.mathematica.index.packageexport; @@ -126,7 +125,7 @@ public boolean equals(Object obj) { @Override public int getHashCode(PackageExportSymbol value) { - return hashCode(); + return value.hashCode(); } @Override diff --git a/src/de/halirutan/mathematica/lang/psi/impl/SymbolImpl.java b/src/de/halirutan/mathematica/lang/psi/impl/SymbolImpl.java index 3e1b8ee2..09c100f1 100644 --- a/src/de/halirutan/mathematica/lang/psi/impl/SymbolImpl.java +++ b/src/de/halirutan/mathematica/lang/psi/impl/SymbolImpl.java @@ -1,24 +1,23 @@ /* - * Copyright (c) 2017 Patrick Scheibe + * Copyright (c) 2018 Patrick Scheibe * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. */ package de.halirutan.mathematica.lang.psi.impl; @@ -65,7 +64,6 @@ public class SymbolImpl extends ExpressionImpl implements Symbol { private static final MathematicaSymbolResolver LOCAL_SYMBOL_RESOLVER = new MathematicaLocalSymbolResolver(); private static final MathematicaGlobalSymbolResolver GLOBAL_SYMBOL_RESOLVER = new MathematicaGlobalSymbolResolver(); - private final ResolveCache myResolveCache = ResolveCache.getInstance(getProject()); private MScope myScope = null; public SymbolImpl(ASTNode node) { @@ -176,6 +174,7 @@ public PsiElement resolve() { public ResolveResult[] multiResolve(boolean incompleteCode) { final PsiFile containingFile = getContainingFile(); if (containingFile != null) { + final ResolveCache myResolveCache = ResolveCache.getInstance(getProject()); final ResolveResult[] localResult = myResolveCache.resolveWithCaching(this, LOCAL_SYMBOL_RESOLVER, true, incompleteCode, containingFile); if (!Arrays.equals(ResolveResult.EMPTY_ARRAY, localResult) && localResult[0] instanceof SymbolResolveResult) { diff --git a/src/de/halirutan/mathematica/lang/psi/util/MathematicaFullFormCreator.java b/src/de/halirutan/mathematica/lang/psi/util/MathematicaFullFormCreator.java deleted file mode 100644 index ffb0b45f..00000000 --- a/src/de/halirutan/mathematica/lang/psi/util/MathematicaFullFormCreator.java +++ /dev/null @@ -1,189 +0,0 @@ -/* - * Copyright (c) 2017 Patrick Scheibe - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - * - */ - -package de.halirutan.mathematica.lang.psi.util; - -import com.intellij.openapi.progress.ProgressIndicatorProvider; -import com.intellij.psi.PsiComment; -import com.intellij.psi.PsiElement; -import com.intellij.psi.PsiFile; -import com.intellij.psi.PsiWhiteSpace; -import de.halirutan.mathematica.lang.psi.MathematicaRecursiveVisitor; -import de.halirutan.mathematica.lang.psi.api.*; -import de.halirutan.mathematica.lang.psi.api.Number; -import de.halirutan.mathematica.lang.psi.api.pattern.Blank; -import de.halirutan.mathematica.lang.psi.api.pattern.BlankNullSequence; -import de.halirutan.mathematica.lang.psi.api.pattern.BlankSequence; -import de.halirutan.mathematica.lang.psi.api.string.MString; -import org.jetbrains.annotations.NotNull; - -import java.util.List; - -/** - * @author patrick (03.09.17). - */ -public class MathematicaFullFormCreator extends MathematicaRecursiveVisitor { - - private final StringBuilder myBuilder = new StringBuilder(); - private int myCharCount = 0; - - @Override - public void visitElement(PsiElement element) { - ProgressIndicatorProvider.checkCanceled(); - if (element instanceof PsiWhiteSpace || element instanceof PsiComment) { - return; - } - addContent(element); - createEnclosedExpression(element.getChildren(), element.getTextLength() > 40 || myCharCount > 120); - } - - @Override - public void visitFile(PsiFile file) { - final PsiElement[] children = file.getChildren(); - for (PsiElement child : children) { - child.accept(this); - addLineFeed(); - addLineFeed(); - } - } - - @Override - public void visitFunctionCall(FunctionCall functionCall) { - functionCall.getHead().accept(this); - final List parameters = functionCall.getParameters(); - PsiElement[] parameterArray = parameters.toArray(new PsiElement[parameters.size()]); - createEnclosedExpression(parameterArray, functionCall.getTextLength() > 30 || myCharCount > 120); - } - - @Override - public void visitSymbol(Symbol symbol) { - addContent(symbol.getText()); - } - - @Override - public void visitNumber(Number number) { - addContent(number.getText()); - } - - @Override - public void visitString(MString mString) { - addContent(mString.getText()); - } - - @Override - public void visitBlank(Blank blank) { - visitBlankLikeElement(blank); - } - - @Override - public void visitBlankSequence(BlankSequence blankSequence) { - visitBlankLikeElement(blankSequence); - } - - @Override - public void visitBlankNullSequence(BlankNullSequence blankNullSequence) { - visitBlankLikeElement(blankNullSequence); - } - - private void visitBlankLikeElement(PsiElement blank) { - if (blank instanceof Blank || blank instanceof BlankSequence || blank instanceof BlankNullSequence) { - String head = blank.toString(); - final PsiElement[] children = blank.getChildren(); - if (children.length < 1 || children.length > 2) { - return; - } - addContent("Pattern["); - children[0].accept(this); - if (children.length == 2) { - addContent("," + head + "["); - children[1].accept(this); - addContent("]]"); - } else { - addContent("," + head + "[]]"); - } - } - - } - - @Override - public void visitGroup(Group group) { - final PsiElement[] children = group.getChildren(); - if (children.length == 1) { - children[0].accept(this); - } - } - - @Override - public void visitStringifiedSymbol(StringifiedSymbol stringifiedSymbol) { - addContent("\"" + stringifiedSymbol.getText() + "\""); - } - - private void createEnclosedExpression(PsiElement[] elements, boolean makeLineBreak) { - if (elements == null) { - addContent("[]"); - } else { - addContent("["); - if (makeLineBreak) { - addLineFeed(); - } - myCharCount++; - for (int i = 0; i < elements.length; i++) { - if (elements[i] instanceof PsiWhiteSpace || elements[i] instanceof PsiComment) { - continue; - } - elements[i].accept(this); - if (i != elements.length - 1) { - addContent(","); - if (makeLineBreak) { - addLineFeed(); - } - } - } - if (makeLineBreak) { - addLineFeed(); - } - addContent("]"); - } - } - - private void addContent(@NotNull PsiElement element) { - addContent(element.toString()); - } - - private void addContent(@NotNull String content) { - myBuilder.append(content); - myCharCount += content.length(); - } - - private void addLineFeed() { - myBuilder.append("\n"); - myCharCount = 0; - } - - public String getFullForm(PsiElement element) { - element.accept(this); - return myBuilder.toString(); - } - - -} diff --git a/src/de/halirutan/mathematica/lang/psi/util/MathematicaFullFormCreator.kt b/src/de/halirutan/mathematica/lang/psi/util/MathematicaFullFormCreator.kt new file mode 100644 index 00000000..b3a023cd --- /dev/null +++ b/src/de/halirutan/mathematica/lang/psi/util/MathematicaFullFormCreator.kt @@ -0,0 +1,183 @@ +/* + * Copyright (c) 2018 Patrick Scheibe + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package de.halirutan.mathematica.lang.psi.util + +import com.intellij.openapi.progress.ProgressIndicatorProvider +import com.intellij.psi.PsiComment +import com.intellij.psi.PsiElement +import com.intellij.psi.PsiFile +import com.intellij.psi.PsiWhiteSpace +import de.halirutan.mathematica.lang.psi.MathematicaRecursiveVisitor +import de.halirutan.mathematica.lang.psi.api.FunctionCall +import de.halirutan.mathematica.lang.psi.api.Group +import de.halirutan.mathematica.lang.psi.api.Number +import de.halirutan.mathematica.lang.psi.api.StringifiedSymbol +import de.halirutan.mathematica.lang.psi.api.Symbol +import de.halirutan.mathematica.lang.psi.api.pattern.Blank +import de.halirutan.mathematica.lang.psi.api.pattern.BlankNullSequence +import de.halirutan.mathematica.lang.psi.api.pattern.BlankSequence +import de.halirutan.mathematica.lang.psi.api.string.MString + +/** + * Aims to provide a FullForm that is close to what Mathematica has. + * We cannot completely reach this goal but this function is helpful to inspect parser errors since it makes precedences + * explicit. + * @author patrick (03.09.17). + */ +class MathematicaFullFormCreator : MathematicaRecursiveVisitor() { + + private val myBuilder = StringBuilder() + private var myCharCount = 0 + + override fun visitElement(element: PsiElement) { + ProgressIndicatorProvider.checkCanceled() + if (element is PsiWhiteSpace || element is PsiComment) { + return + } + addContent(element) + createEnclosedExpression(element.children, element.textLength > 40 || myCharCount > 120) + } + + override fun visitFile(file: PsiFile) { + val children = file.children + for (child in children) { + if (child is PsiWhiteSpace || child is PsiComment) { + continue + } + child.accept(this) + addLineFeed() + addLineFeed() + } + } + + override fun visitFunctionCall(functionCall: FunctionCall) { + functionCall.head.accept(this) + val parameters = functionCall.parameters + val parameterArray = parameters.toTypedArray() + createEnclosedExpression(parameterArray, functionCall.textLength > 30 || myCharCount > 120) + } + + override fun visitSymbol(symbol: Symbol) { + addContent(symbol.text) + } + + override fun visitNumber(number: Number) { + addContent(number.text) + } + + override fun visitString(mString: MString) { + addContent(mString.text) + } + + override fun visitBlank(blank: Blank) { + visitBlankLikeElement(blank) + } + + override fun visitBlankSequence(blankSequence: BlankSequence) { + visitBlankLikeElement(blankSequence) + } + + override fun visitBlankNullSequence(blankNullSequence: BlankNullSequence) { + visitBlankLikeElement(blankNullSequence) + } + + private fun visitBlankLikeElement(blank: PsiElement) { + if (blank is Blank || blank is BlankSequence || blank is BlankNullSequence) { + val head = blank.toString() + val children = blank.children + when (children.size) { + 0 -> addContent("$head[]") + 1, 2 -> { + addContent("Pattern[") + children[0].accept(this) + if (children.size == 2) { + addContent(",$head[") + children[1].accept(this) + addContent("]") + } + addContent("]") + } + } + } + } + + override fun visitGroup(group: Group) { + val children = group.children + if (children.size == 1) { + children[0].accept(this) + } + } + + override fun visitStringifiedSymbol(stringifiedSymbol: StringifiedSymbol) { + addContent("\"" + stringifiedSymbol.text + "\"") + } + + private fun createEnclosedExpression(elements: Array?, makeLineBreak: Boolean) { + if (elements == null) { + addContent("[]") + } else { + addContent("[") + if (makeLineBreak) { + addLineFeed() + } + myCharCount++ + for (i in elements.indices) { + if (elements[i] is PsiWhiteSpace || elements[i] is PsiComment) { + continue + } + elements[i].accept(this) + if (i != elements.size - 1) { + addContent(",") + if (makeLineBreak) { + addLineFeed() + } + } + } + if (makeLineBreak) { + addLineFeed() + } + addContent("]") + } + } + + private fun addContent(element: PsiElement) { + addContent(element.toString()) + } + + private fun addContent(content: String) { + myBuilder.append(content) + myCharCount += content.length + } + + private fun addLineFeed() { + myBuilder.append("\n") + myCharCount = 0 + } + + fun getFullForm(element: PsiElement): String { + element.accept(this) + return myBuilder.toString() + } + + +} diff --git a/src/de/halirutan/mathematica/lang/resolve/MathematicaGlobalSymbolResolver.java b/src/de/halirutan/mathematica/lang/resolve/MathematicaGlobalSymbolResolver.java index abee910e..7b51c0ca 100644 --- a/src/de/halirutan/mathematica/lang/resolve/MathematicaGlobalSymbolResolver.java +++ b/src/de/halirutan/mathematica/lang/resolve/MathematicaGlobalSymbolResolver.java @@ -1,30 +1,30 @@ /* - * Copyright (c) 2017 Patrick Scheibe + * Copyright (c) 2018 Patrick Scheibe * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. */ package de.halirutan.mathematica.lang.resolve; import com.intellij.openapi.module.Module; import com.intellij.openapi.module.ModuleUtilCore; +import com.intellij.openapi.progress.ProgressManager; import com.intellij.openapi.project.Project; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.psi.PsiFile; @@ -34,6 +34,7 @@ import com.intellij.psi.search.ProjectScope; import com.intellij.psi.util.PsiTreeUtil; import com.intellij.util.indexing.FileBasedIndex; +import com.intellij.util.indexing.IdFilter; import de.halirutan.mathematica.index.packageexport.MathematicaPackageExportIndex; import de.halirutan.mathematica.lang.psi.api.MathematicaPsiFile; import de.halirutan.mathematica.lang.psi.api.Symbol; @@ -100,6 +101,7 @@ public ResolveResult[] resolve(@NotNull Symbol ref, @NotNull PsiFile containingF fileIndex.processAllKeys( MathematicaPackageExportIndex.INDEX_ID, key -> { + ProgressManager.checkCanceled(); if (key.isExported() && key.getSymbol().equals(ref.getSymbolName())) { final Collection containingFiles = fileIndex.getContainingFiles(MathematicaPackageExportIndex.INDEX_ID, key, moduleScope); @@ -116,7 +118,10 @@ public ResolveResult[] resolve(@NotNull Symbol ref, @NotNull PsiFile containingF }); } return true; - }, moduleScope, null); + }, + moduleScope, + IdFilter.getProjectIdFilter(project, false) + ); fileIndex.getAllKeys(MathematicaPackageExportIndex.INDEX_ID, project); if (!references.isEmpty()) { return references.toArray(new ResolveResult[0]); diff --git a/src/de/halirutan/mathematica/lang/resolve/MathematicaReferenceContributor.java b/src/de/halirutan/mathematica/lang/resolve/MathematicaReferenceContributor.java index 4710fd07..32f6f894 100644 --- a/src/de/halirutan/mathematica/lang/resolve/MathematicaReferenceContributor.java +++ b/src/de/halirutan/mathematica/lang/resolve/MathematicaReferenceContributor.java @@ -1,5 +1,6 @@ /* - * Copyright (c) 2017 Patrick Scheibe + * Copyright (c) 2018 Patrick Scheibe + * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights @@ -7,16 +8,16 @@ * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. */ package de.halirutan.mathematica.lang.resolve; @@ -38,8 +39,6 @@ public class MathematicaReferenceContributor extends PsiReferenceContributor { */ @Override public void registerReferenceProviders(@NotNull PsiReferenceRegistrar registrar) { -// registrar.registerReferenceProvider(PlatformPatterns.psiElement(Symbol.class), -// new MathematicaSymbolReferenceProvider()); registrar.registerReferenceProvider(PlatformPatterns.psiElement(MString.class), new MathematicaStringReferenceProvider()); }