diff --git a/build.gradle.kts b/build.gradle.kts index aa6efd240..f659673b7 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -127,7 +127,7 @@ intellij { pluginName = "TeXiFy-IDEA" // indices plugin doesn't work in tests - setPlugins("tanvd.grazi", "java", "com.firsttimeinforever.intellij.pdf.viewer.intellij-pdf-viewer:0.10.1") // , "com.jetbrains.hackathon.indices.viewer:1.12") + setPlugins("tanvd.grazi", "com.firsttimeinforever.intellij.pdf.viewer.intellij-pdf-viewer:0.10.2") // , "com.jetbrains.hackathon.indices.viewer:1.12") // Use the since build number from plugin.xml updateSinceUntilBuild = false @@ -138,6 +138,7 @@ intellij { // Docs: https://github.com/JetBrains/gradle-intellij-plugin#intellij-platform-properties // All snapshot versions: https://www.jetbrains.com/intellij-repository/snapshots/ version = "2020.3.1" +// version = "PY-2020.3.3" // version = "PY-203.5419.8-EAP-SNAPSHOT" // type = "PY" diff --git a/gen/nl/hannahsten/texifyidea/psi/BibtexEntry.java b/gen/nl/hannahsten/texifyidea/psi/BibtexEntry.java index 9c6321036..cc5f14434 100644 --- a/gen/nl/hannahsten/texifyidea/psi/BibtexEntry.java +++ b/gen/nl/hannahsten/texifyidea/psi/BibtexEntry.java @@ -47,6 +47,6 @@ public interface BibtexEntry extends PsiNameIdentifierOwner, StubBasedPsiElement String getName(); - PsiElement setName(@NonNls @NotNull String name); + PsiElement setName(@NotNull @NonNls String name); } diff --git a/gen/nl/hannahsten/texifyidea/psi/BibtexId.java b/gen/nl/hannahsten/texifyidea/psi/BibtexId.java index 3bb41de9d..21df9817a 100644 --- a/gen/nl/hannahsten/texifyidea/psi/BibtexId.java +++ b/gen/nl/hannahsten/texifyidea/psi/BibtexId.java @@ -17,4 +17,6 @@ public interface BibtexId extends PsiNameIdentifierOwner { PsiElement setName(String name); + void delete(); + } diff --git a/gen/nl/hannahsten/texifyidea/psi/LatexParameterText.java b/gen/nl/hannahsten/texifyidea/psi/LatexParameterText.java index db032f2b7..4c1f8e41d 100644 --- a/gen/nl/hannahsten/texifyidea/psi/LatexParameterText.java +++ b/gen/nl/hannahsten/texifyidea/psi/LatexParameterText.java @@ -22,4 +22,6 @@ public interface LatexParameterText extends PsiNameIdentifierOwner { PsiElement setName(String name); + void delete(); + } diff --git a/gen/nl/hannahsten/texifyidea/psi/impl/BibtexEntryImpl.java b/gen/nl/hannahsten/texifyidea/psi/impl/BibtexEntryImpl.java index a6132db01..00d0cb962 100644 --- a/gen/nl/hannahsten/texifyidea/psi/impl/BibtexEntryImpl.java +++ b/gen/nl/hannahsten/texifyidea/psi/impl/BibtexEntryImpl.java @@ -121,7 +121,7 @@ public String getName() { } @Override - public PsiElement setName(@NonNls @NotNull String name) { + public PsiElement setName(@NotNull @NonNls String name) { return BibtexPsiImplUtil.setName(this, name); } diff --git a/gen/nl/hannahsten/texifyidea/psi/impl/BibtexIdImpl.java b/gen/nl/hannahsten/texifyidea/psi/impl/BibtexIdImpl.java index d8817c93b..d399439b9 100644 --- a/gen/nl/hannahsten/texifyidea/psi/impl/BibtexIdImpl.java +++ b/gen/nl/hannahsten/texifyidea/psi/impl/BibtexIdImpl.java @@ -48,4 +48,9 @@ public PsiElement setName(String name) { return BibtexPsiImplUtil.setName(this, name); } + @Override + public void delete() { + BibtexPsiImplUtil.delete(this); + } + } diff --git a/gen/nl/hannahsten/texifyidea/psi/impl/LatexParameterTextImpl.java b/gen/nl/hannahsten/texifyidea/psi/impl/LatexParameterTextImpl.java index 0d63111d3..fc2ad5ec9 100644 --- a/gen/nl/hannahsten/texifyidea/psi/impl/LatexParameterTextImpl.java +++ b/gen/nl/hannahsten/texifyidea/psi/impl/LatexParameterTextImpl.java @@ -59,4 +59,9 @@ public PsiElement setName(String name) { return LatexPsiImplUtil.setName(this, name); } + @Override + public void delete() { + LatexPsiImplUtil.delete(this); + } + } diff --git a/src/nl/hannahsten/texifyidea/LatexParserDefinition.kt b/src/nl/hannahsten/texifyidea/LatexParserDefinition.kt index 04fd18cf7..178bccd99 100644 --- a/src/nl/hannahsten/texifyidea/LatexParserDefinition.kt +++ b/src/nl/hannahsten/texifyidea/LatexParserDefinition.kt @@ -52,7 +52,7 @@ class LatexParserDefinition : ParserDefinition { val FILE: IStubFileElementType<*> = object : IStubFileElementType( Language.findInstance(LatexLanguage::class.java) ) { - override fun getStubVersion(): Int = 20 + override fun getStubVersion(): Int = 21 } } diff --git a/src/nl/hannahsten/texifyidea/grammar/Bibtex.bnf b/src/nl/hannahsten/texifyidea/grammar/Bibtex.bnf index c8f213df0..a0201d56b 100644 --- a/src/nl/hannahsten/texifyidea/grammar/Bibtex.bnf +++ b/src/nl/hannahsten/texifyidea/grammar/Bibtex.bnf @@ -35,7 +35,7 @@ type ::= TYPE_TOKEN preamble ::= (quoted_string (CONCATENATE quoted_string)*) | NUMBER | IDENTIFIER id ::= comment* IDENTIFIER comment* { - methods=[getNameIdentifier getName setName] + methods=[getNameIdentifier getName setName delete] } tag ::= comment* key comment* ASSIGNMENT comment* content comment* { diff --git a/src/nl/hannahsten/texifyidea/grammar/Latex.bnf b/src/nl/hannahsten/texifyidea/grammar/Latex.bnf index 749e217ba..35effd56b 100644 --- a/src/nl/hannahsten/texifyidea/grammar/Latex.bnf +++ b/src/nl/hannahsten/texifyidea/grammar/Latex.bnf @@ -139,7 +139,7 @@ keyval_content ::= parameter_text | parameter_group // This assumes that parameter text which is a reference, appears directly under param_content // Commands is here instead of in required_param_content because it can be part of reference text for example to a file parameter_text ::= (commands | NORMAL_TEXT_WORD | STAR | AMPERSAND | OPEN_ANGLE_BRACKET | CLOSE_ANGLE_BRACKET | QUOTATION_MARK | PIPE | EXCLAMATION_MARK)+ { - methods=[getReferences getReference getNameIdentifier getName setName] + methods=[getReferences getReference getNameIdentifier getName setName delete] } group ::= OPEN_BRACE content CLOSE_BRACE { pin=1 } diff --git a/src/nl/hannahsten/texifyidea/inspections/SafeDeleteFix.kt b/src/nl/hannahsten/texifyidea/inspections/SafeDeleteFix.kt new file mode 100644 index 000000000..46346fcd0 --- /dev/null +++ b/src/nl/hannahsten/texifyidea/inspections/SafeDeleteFix.kt @@ -0,0 +1,55 @@ +/* + * Copyright 2000-2009 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package nl.hannahsten.texifyidea.inspections + +import com.intellij.codeInsight.FileModificationService +import com.intellij.codeInspection.LocalQuickFixAndIntentionActionOnPsiElement +import com.intellij.openapi.editor.Editor +import com.intellij.openapi.project.Project +import com.intellij.psi.PsiElement +import com.intellij.psi.PsiFile +import com.intellij.refactoring.safeDelete.SafeDeleteHandler + +/** + * Source: com.intellij.codeInsight.daemon.impl.quickfix.SafeDeleteFix + */ +open class SafeDeleteFix(element: PsiElement) : LocalQuickFixAndIntentionActionOnPsiElement(element) { + + override fun getText(): String { + val startElement = startElement + return "Safe delete " + startElement.text + } + + override fun getFamilyName(): String { + return "Safe delete" + } + + override fun invoke( + project: Project, + file: PsiFile, + editor: Editor?, + startElement: PsiElement, + endElement: PsiElement + ) { + if (!FileModificationService.getInstance().prepareFileForWrite(file)) return + val elements = arrayOf(startElement) + SafeDeleteHandler.invoke(project, elements, true) + } + + override fun startInWriteAction(): Boolean { + return false + } +} \ No newline at end of file diff --git a/src/nl/hannahsten/texifyidea/inspections/bibtex/BibtexUnusedEntryInspection.kt b/src/nl/hannahsten/texifyidea/inspections/bibtex/BibtexUnusedEntryInspection.kt index 01320f5bb..696ad26b1 100644 --- a/src/nl/hannahsten/texifyidea/inspections/bibtex/BibtexUnusedEntryInspection.kt +++ b/src/nl/hannahsten/texifyidea/inspections/bibtex/BibtexUnusedEntryInspection.kt @@ -1,22 +1,19 @@ package nl.hannahsten.texifyidea.inspections.bibtex import com.intellij.codeInspection.InspectionManager -import com.intellij.codeInspection.LocalQuickFix import com.intellij.codeInspection.ProblemDescriptor import com.intellij.codeInspection.ProblemHighlightType -import com.intellij.openapi.project.Project +import com.intellij.psi.PsiElement import com.intellij.psi.PsiFile -import com.intellij.psi.PsiWhiteSpace import com.intellij.psi.SmartPsiElementPointer -import com.intellij.psi.search.GlobalSearchScope import com.intellij.psi.search.searches.ReferencesSearch import com.intellij.refactoring.suggested.createSmartPointer -import nl.hannahsten.texifyidea.index.BibtexEntryIndex +import nl.hannahsten.texifyidea.inspections.SafeDeleteFix import nl.hannahsten.texifyidea.inspections.InsightGroup import nl.hannahsten.texifyidea.inspections.TexifyInspectionBase import nl.hannahsten.texifyidea.psi.BibtexId import nl.hannahsten.texifyidea.util.childrenOfType -import nl.hannahsten.texifyidea.util.previousSiblingOfType +import org.jetbrains.annotations.NotNull class BibtexUnusedEntryInspection : TexifyInspectionBase() { @@ -39,18 +36,8 @@ class BibtexUnusedEntryInspection : TexifyInspectionBase() { } .toList() - class RemoveBibtexEntryFix(private val id: SmartPsiElementPointer) : LocalQuickFix { + class RemoveBibtexEntryFix(private val id: SmartPsiElementPointer) : SafeDeleteFix(id.element as @NotNull PsiElement) { - override fun getFamilyName(): String = "Remove BibTeX entry ${id.element?.text}" - - override fun applyFix(project: Project, descriptor: ProblemDescriptor) { - val text = id.element?.text ?: return - - val searchScope = GlobalSearchScope.fileScope(descriptor.psiElement.containingFile) - BibtexEntryIndex.getEntryByName(text, project, searchScope).forEach { - it.previousSiblingOfType(PsiWhiteSpace::class)?.let { w -> w.parent.node.removeChild(w.node) } - it.parent.node.removeChild(it.node) - } - } + override fun getText(): String = "Safe delete ${id.element?.text}" } } \ No newline at end of file diff --git a/src/nl/hannahsten/texifyidea/inspections/latex/LatexFigureNotReferencedInspection.kt b/src/nl/hannahsten/texifyidea/inspections/latex/LatexFigureNotReferencedInspection.kt index 4b6e86fd4..e86181a36 100644 --- a/src/nl/hannahsten/texifyidea/inspections/latex/LatexFigureNotReferencedInspection.kt +++ b/src/nl/hannahsten/texifyidea/inspections/latex/LatexFigureNotReferencedInspection.kt @@ -3,15 +3,20 @@ package nl.hannahsten.texifyidea.inspections.latex import com.intellij.codeInspection.InspectionManager import com.intellij.codeInspection.ProblemDescriptor import com.intellij.codeInspection.ProblemHighlightType +import com.intellij.psi.PsiElement import com.intellij.psi.PsiFile +import com.intellij.psi.SmartPsiElementPointer +import com.intellij.refactoring.suggested.createSmartPointer import nl.hannahsten.texifyidea.inspections.InsightGroup +import nl.hannahsten.texifyidea.inspections.SafeDeleteFix import nl.hannahsten.texifyidea.inspections.TexifyInspectionBase import nl.hannahsten.texifyidea.lang.magic.MagicCommentScope import nl.hannahsten.texifyidea.psi.LatexCommands +import nl.hannahsten.texifyidea.psi.LatexParameterText import nl.hannahsten.texifyidea.util.* import nl.hannahsten.texifyidea.util.files.commandsInFileSet import nl.hannahsten.texifyidea.util.magic.CommandMagic -import nl.hannahsten.texifyidea.util.magic.EnvironmentMagic +import org.jetbrains.annotations.NotNull import java.util.* open class LatexFigureNotReferencedInspection : TexifyInspectionBase() { @@ -22,7 +27,7 @@ open class LatexFigureNotReferencedInspection : TexifyInspectionBase() { override val outerSuppressionScopes = EnumSet.of(MagicCommentScope.GROUP)!! - override fun getDisplayName(): String = "Figure Not Referenced" + override fun getDisplayName(): String = "Figure not referenced" override fun inspectFile(file: PsiFile, manager: InspectionManager, isOntheFly: Boolean): MutableList { val figureLabels = getFigureLabels(file) @@ -31,7 +36,7 @@ open class LatexFigureNotReferencedInspection : TexifyInspectionBase() { val descriptors = descriptorList() for (label in figureLabels.values) { - descriptors.add(createDescriptor(manager, label, isOntheFly)) + descriptors.add(createDescriptor(manager, label, isOntheFly) ?: continue) } return descriptors @@ -49,26 +54,32 @@ open class LatexFigureNotReferencedInspection : TexifyInspectionBase() { } } - private fun createDescriptor(manager: InspectionManager, label: LatexCommands, isOntheFly: Boolean): ProblemDescriptor = - manager.createProblemDescriptor( - label, - "Figure is not referenced", - isOntheFly, - emptyArray(), - ProblemHighlightType.GENERIC_ERROR_OR_WARNING - ) + private fun createDescriptor(manager: InspectionManager, label: LatexCommands, isOntheFly: Boolean): ProblemDescriptor? = + label.firstChildOfType(LatexParameterText::class)?.let { + manager.createProblemDescriptor( + it, + "Figure is not referenced", + RemoveFigureFix(it.createSmartPointer()), + ProblemHighlightType.LIKE_UNUSED_SYMBOL, + isOntheFly + ) + } /** * Find all commands in the file that label a figure. */ private fun getFigureLabels(file: PsiFile): MutableMap = file.findLabelingCommandsInFileAsSequence() - .filter(this::isFigureLabel) + .filter(LatexCommands::isFigureLabel) .associateBy(LatexCommands::labelName) .toMutableMap() - private fun isFigureLabel(label: LatexCommands): Boolean = - label.inDirectEnvironment(EnvironmentMagic.figures) + class RemoveFigureFix(label: SmartPsiElementPointer) : SafeDeleteFix(label.element as @NotNull PsiElement) { + + override fun getText(): String { + return "Safe delete figure environment" + } + } } private val LatexCommands.labelName: String? @@ -76,3 +87,5 @@ private val LatexCommands.labelName: String? private val LatexCommands.referencedLabelNames: List get() = requiredParameter(0)?.split(",") ?: emptyList() + +fun dummy() = Unit diff --git a/src/nl/hannahsten/texifyidea/psi/BibtexIdUtil.kt b/src/nl/hannahsten/texifyidea/psi/BibtexIdUtil.kt index 7c02a8f55..c173c36b2 100644 --- a/src/nl/hannahsten/texifyidea/psi/BibtexIdUtil.kt +++ b/src/nl/hannahsten/texifyidea/psi/BibtexIdUtil.kt @@ -2,8 +2,11 @@ package nl.hannahsten.texifyidea.psi import com.intellij.psi.PsiElement import com.intellij.psi.PsiFileFactory +import com.intellij.psi.search.GlobalSearchScope import nl.hannahsten.texifyidea.BibtexLanguage +import nl.hannahsten.texifyidea.index.BibtexEntryIndex import nl.hannahsten.texifyidea.util.firstParentOfType +import nl.hannahsten.texifyidea.util.remove fun getNameIdentifier(element: BibtexId): PsiElement { return element @@ -21,4 +24,13 @@ fun setName(element: BibtexId, name: String): PsiElement { fun getName(element: BibtexId): String { return element.text +} + +fun delete(element: BibtexId) { + val text = element.text ?: return + + val searchScope = GlobalSearchScope.fileScope(element.containingFile) + BibtexEntryIndex.getEntryByName(text, element.project, searchScope).forEach { + it.remove() + } } \ No newline at end of file diff --git a/src/nl/hannahsten/texifyidea/psi/BibtexPsiImplUtil.java b/src/nl/hannahsten/texifyidea/psi/BibtexPsiImplUtil.java index 19ff5c024..3abd57eb9 100644 --- a/src/nl/hannahsten/texifyidea/psi/BibtexPsiImplUtil.java +++ b/src/nl/hannahsten/texifyidea/psi/BibtexPsiImplUtil.java @@ -101,6 +101,10 @@ public static String getName(@NotNull BibtexId element) { return BibtexIdUtilKt.getName(element); } + public static void delete(@NotNull BibtexId element) { + BibtexIdUtilKt.delete(element); + } + /* * BibtexTag */ diff --git a/src/nl/hannahsten/texifyidea/psi/LatexCommandsImplUtil.kt b/src/nl/hannahsten/texifyidea/psi/LatexCommandsImplUtil.kt index 98f09af90..a81ad95af 100644 --- a/src/nl/hannahsten/texifyidea/psi/LatexCommandsImplUtil.kt +++ b/src/nl/hannahsten/texifyidea/psi/LatexCommandsImplUtil.kt @@ -16,11 +16,9 @@ import nl.hannahsten.texifyidea.lang.commands.RequiredFileArgument import nl.hannahsten.texifyidea.reference.CommandDefinitionReference import nl.hannahsten.texifyidea.reference.InputFileReference import nl.hannahsten.texifyidea.reference.LatexLabelReference -import nl.hannahsten.texifyidea.util.getLabelReferenceCommands +import nl.hannahsten.texifyidea.util.* import nl.hannahsten.texifyidea.util.magic.CommandMagic import nl.hannahsten.texifyidea.util.magic.PatternMagic -import nl.hannahsten.texifyidea.util.requiredParameters -import nl.hannahsten.texifyidea.util.shrink import java.util.* import java.util.regex.Pattern @@ -146,7 +144,7 @@ fun stripGroup(text: String): String { } /** - * Generates a map of parameter names and values (assuming they are in the form []name=]value) for all optional parameters, comma-separated and separate optional parameters are treated equally. + * Generates a map of parameter names and values (assuming they are in the form name=value) for all optional parameters, comma-separated and separate optional parameters are treated equally. * If a value does not have a name, the value will be the key in the hashmap mapping to the empty string. */ // Explicitly use a LinkedHashMap to preserve iteration order diff --git a/src/nl/hannahsten/texifyidea/psi/LatexParameterTextUtil.kt b/src/nl/hannahsten/texifyidea/psi/LatexParameterTextUtil.kt index bbbf138df..8edf7c3b9 100644 --- a/src/nl/hannahsten/texifyidea/psi/LatexParameterTextUtil.kt +++ b/src/nl/hannahsten/texifyidea/psi/LatexParameterTextUtil.kt @@ -127,3 +127,14 @@ val LatexParameterText.command: PsiElement? get() { return this.firstParentOfType(LatexCommands::class)?.firstChild } + +fun delete(element: LatexParameterText) { + val cmd = element.parentOfType(LatexCommands::class) ?: return + if (cmd.isFigureLabel()) { + // Look for the NoMathContent that is around the environment, because that is the PsiElement that has the + // whitespace and other normal text as siblings. + cmd.parentOfType(LatexEnvironment::class) + ?.parentOfType(LatexNoMathContent::class) + ?.remove() + } +} diff --git a/src/nl/hannahsten/texifyidea/psi/LatexPsiImplUtil.java b/src/nl/hannahsten/texifyidea/psi/LatexPsiImplUtil.java index eca8eb4f9..89012dd20 100644 --- a/src/nl/hannahsten/texifyidea/psi/LatexPsiImplUtil.java +++ b/src/nl/hannahsten/texifyidea/psi/LatexPsiImplUtil.java @@ -136,6 +136,10 @@ public static String getName(@NotNull LatexParameterText element) { return LatexParameterTextUtilKt.getName(element); } + + public static void delete(@NotNull LatexParameterText element) { + LatexParameterTextUtilKt.delete(element); + } /* * LatexParameter */ diff --git a/src/nl/hannahsten/texifyidea/refactoring/BibtexRefactoringSupportProvider.kt b/src/nl/hannahsten/texifyidea/refactoring/BibtexRefactoringSupportProvider.kt index 7cc2485ef..3696f76e2 100644 --- a/src/nl/hannahsten/texifyidea/refactoring/BibtexRefactoringSupportProvider.kt +++ b/src/nl/hannahsten/texifyidea/refactoring/BibtexRefactoringSupportProvider.kt @@ -2,6 +2,7 @@ package nl.hannahsten.texifyidea.refactoring import com.intellij.lang.refactoring.RefactoringSupportProvider import com.intellij.psi.PsiElement +import nl.hannahsten.texifyidea.psi.BibtexId /** * This class is used to enable inline refactoring. @@ -12,4 +13,8 @@ class BibtexRefactoringSupportProvider : RefactoringSupportProvider() { // Inline refactoring is not enabled for BibtexId because it will not handle the comma separator correctly when the bibtex id is renamed directly (not from usages). return false } + + override fun isSafeDeleteAvailable(element: PsiElement): Boolean { + return element is BibtexId + } } \ No newline at end of file diff --git a/src/nl/hannahsten/texifyidea/refactoring/LatexRefactoringSupportProvider.kt b/src/nl/hannahsten/texifyidea/refactoring/LatexRefactoringSupportProvider.kt index 1e9d66901..14084c070 100644 --- a/src/nl/hannahsten/texifyidea/refactoring/LatexRefactoringSupportProvider.kt +++ b/src/nl/hannahsten/texifyidea/refactoring/LatexRefactoringSupportProvider.kt @@ -16,4 +16,8 @@ class LatexRefactoringSupportProvider : RefactoringSupportProvider() { else -> false } } + + override fun isSafeDeleteAvailable(element: PsiElement): Boolean { + return element is LatexParameterText + } } \ No newline at end of file diff --git a/src/nl/hannahsten/texifyidea/util/Commands.kt b/src/nl/hannahsten/texifyidea/util/Commands.kt index 4c7a51622..caae3f1d3 100644 --- a/src/nl/hannahsten/texifyidea/util/Commands.kt +++ b/src/nl/hannahsten/texifyidea/util/Commands.kt @@ -12,6 +12,7 @@ import nl.hannahsten.texifyidea.psi.LatexCommands import nl.hannahsten.texifyidea.psi.LatexParameter import nl.hannahsten.texifyidea.psi.LatexPsiHelper import nl.hannahsten.texifyidea.util.magic.CommandMagic +import nl.hannahsten.texifyidea.util.magic.EnvironmentMagic /** * Finds all defined commands within the project. @@ -63,4 +64,7 @@ fun LatexParameter.indexOf() = indexOfChildByType */ fun LatexCommands.defaultCommand(): LatexCommand? { return LatexCommand.lookup(this.name)?.firstOrNull() -} \ No newline at end of file +} + +fun LatexCommands.isFigureLabel(): Boolean = + name in project.getLabelDefinitionCommands() && inDirectEnvironment(EnvironmentMagic.figures) \ No newline at end of file diff --git a/src/nl/hannahsten/texifyidea/util/Psi.kt b/src/nl/hannahsten/texifyidea/util/Psi.kt index ca0a0e7f0..a5e791a6a 100644 --- a/src/nl/hannahsten/texifyidea/util/Psi.kt +++ b/src/nl/hannahsten/texifyidea/util/Psi.kt @@ -383,6 +383,20 @@ fun PsiElement.firstChildIgnoringWhitespaceOrNull(): PsiElement? { fun PsiElement?.isMagicComment(): Boolean = this?.text?.let { t -> TextBasedMagicCommentParser.COMMENT_PREFIX.containsMatchIn(t) } ?: false +/** + * Remove a psi element from the psi tree. + * + * Also remove the white space in front of the psi element when [removeLeadingWhiteSpace] is true, so we don't end up + * with ridiculous amounts of white space. + */ +fun PsiElement.remove(removeLeadingWhiteSpace: Boolean = true) { + if (removeLeadingWhiteSpace) { + previousSiblingOfType(PsiWhiteSpace::class)?.let { + it.parent.node.removeChild(it.node) + } + } + parent.node.removeChild(node) +} /** * Get a sequence of all the parents of this PsiElement with the given type. */ diff --git a/test/nl/hannahsten/texifyidea/inspections/latex/LatexFigureNotReferencedInspectionTest.kt b/test/nl/hannahsten/texifyidea/inspections/latex/LatexFigureNotReferencedInspectionTest.kt index 990311263..32b9b0c10 100644 --- a/test/nl/hannahsten/texifyidea/inspections/latex/LatexFigureNotReferencedInspectionTest.kt +++ b/test/nl/hannahsten/texifyidea/inspections/latex/LatexFigureNotReferencedInspectionTest.kt @@ -13,7 +13,7 @@ class LatexFigureNotReferencedInspectionTest : TexifyInspectionTestBase(LatexFig \usepackage{listings} \begin{document} \begin{figure} - \label{fig:some-figure} + \label{fig:some-figure} \end{figure} \end{document} """.trimIndent()