Skip to content

Commit

Permalink
Fix the intention to add block IDs to recognize the current section (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
ahus1 committed Dec 1, 2024
1 parent e4d6b0e commit 342e72a
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ This document provides a high-level view of the changes introduced by release.
- Fix text color of non-icon admonitions in dark preview
- Adding an intent to add the option to disable validation on individual code blocks (#830)
- Be more restrictive when highlighting missing images (#1727)
- Fix the intention to add block IDs to recognize the current section (#1681)

=== 0.43.3

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class AsciiDocAddBlockIdToSection extends Intention {
public class AsciiDocAddBlockIdToSectionIntention extends Intention {

@Override
public @NotNull IntentionPreviewInfo generatePreview(@NotNull Project project, @NotNull Editor editor, @NotNull PsiFile file) {
Expand Down Expand Up @@ -50,20 +50,23 @@ public static AsciiDocSection getSectionWithoutBlockIdAtCursor(PsiFile file, Edi
// cursor is at the very end of the file, and there is document content, move one character
statementAtCaret = file.findElementAt(editor.getSelectionModel().getSelectionStart() - 1);
}
if (statementAtCaret == null) {
return null;
}
while (statementAtCaret instanceof PsiWhiteSpace) {
statementAtCaret = statementAtCaret.getPrevSibling();
if (statementAtCaret == null) {
if (statementAtCaret.getTextOffset() == 0) {
return null;
}
statementAtCaret = file.findElementAt(statementAtCaret.getTextOffset() - 1);
}
if (statementAtCaret == null) {
return null;
}
if (statementAtCaret.getNode() != null && statementAtCaret.getNode().getElementType() == AsciiDocTokenTypes.HEADING_TOKEN) {
statementAtCaret = statementAtCaret.getParent();
}
if (!(statementAtCaret instanceof AsciiDocSection)) {
return null;
while (!(statementAtCaret instanceof AsciiDocSection)) {
statementAtCaret = statementAtCaret.getParent();
if (statementAtCaret == null) {
return null;
}
}
AsciiDocSection section = (AsciiDocSection) statementAtCaret;
if (section.getBlockId() != null) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/AsciiDocBundle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -860,8 +860,8 @@ asciidoc.extract.intention.name=Extract Include Directive
asciidoc.inline.intention.family.name=Inline Include Directive
asciidoc.inline.intention.name=Inline Include Directive
asciidoc.inline.label=Inline {0}:
asciidoc.add.block.id.to.section.family.name=Add Block ID to section
asciidoc.add.block.id.to.section.name=Add Block ID to section
asciidoc.add.block.id.to.section.intention.family.name=Add Block ID to section
asciidoc.add.block.id.to.section.intention.name=Add Block ID to section
asciidoc.admonition.to.block.intention.family.name=Refactor to block admonition
asciidoc.admonition.to.block.intention.name=Refactor to block admonition

Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@
<language>AsciiDoc</language>
<bundleName>AsciiDocBundle</bundleName>
<categoryKey>asciidoc.intention.category</categoryKey>
<className>org.asciidoc.intellij.actions.intentions.AsciiDocAddBlockIdToSection</className>
<className>org.asciidoc.intellij.actions.intentions.AsciiDocAddBlockIdToSectionIntention</className>
</intentionAction>
<renameInputValidator implementation="org.asciidoc.intellij.namesValidator.AsciiDocRenameInputValidator"/>
<lang.namesValidator language="AsciiDoc" implementationClass="org.asciidoc.intellij.namesValidator.AsciiDocNamesValidator"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.asciidoc.intellij.intentions;

import org.asciidoc.intellij.inspections.AsciiDocQuickFixTestBase;

/**
* @author Alexander Schwartz ([email protected])
*/
public class AsciiDocAddBlockIdToSectionIntentionTest extends AsciiDocQuickFixTestBase {

@Override
public void setUp() throws Exception {
super.setUp();
}

public void testSubsection() {
doTest("Add Block ID to section", false);
}

@Override
protected String getBasePath() {
return "intentions/blockIdToSection";
}
}
6 changes: 6 additions & 0 deletions testData/intentions/blockIdToSection/subsection-after.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
= Dummy page

[#_dummy_paragraph]
== Dummy Paragraph
some dummy text
5 changes: 5 additions & 0 deletions testData/intentions/blockIdToSection/subsection.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
= Dummy page

== Dummy Paragraph

some<caret> dummy text

0 comments on commit 342e72a

Please sign in to comment.