diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc
index 800f59447..408054112 100644
--- a/CHANGELOG.adoc
+++ b/CHANGELOG.adoc
@@ -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
diff --git a/src/main/java/org/asciidoc/intellij/actions/intentions/AsciiDocAddBlockIdToSection.java b/src/main/java/org/asciidoc/intellij/actions/intentions/AsciiDocAddBlockIdToSectionIntention.java
similarity index 90%
rename from src/main/java/org/asciidoc/intellij/actions/intentions/AsciiDocAddBlockIdToSection.java
rename to src/main/java/org/asciidoc/intellij/actions/intentions/AsciiDocAddBlockIdToSectionIntention.java
index 1aad0540d..65866d302 100644
--- a/src/main/java/org/asciidoc/intellij/actions/intentions/AsciiDocAddBlockIdToSection.java
+++ b/src/main/java/org/asciidoc/intellij/actions/intentions/AsciiDocAddBlockIdToSectionIntention.java
@@ -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) {
@@ -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) {
diff --git a/src/main/resources/AsciiDocBundle.properties b/src/main/resources/AsciiDocBundle.properties
index 6fd69b6dc..4cae6e767 100644
--- a/src/main/resources/AsciiDocBundle.properties
+++ b/src/main/resources/AsciiDocBundle.properties
@@ -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
diff --git a/src/main/resources/META-INF/plugin.xml b/src/main/resources/META-INF/plugin.xml
index bb03a02cc..28d5ff45f 100644
--- a/src/main/resources/META-INF/plugin.xml
+++ b/src/main/resources/META-INF/plugin.xml
@@ -400,7 +400,7 @@
AsciiDoc
AsciiDocBundle
asciidoc.intention.category
- org.asciidoc.intellij.actions.intentions.AsciiDocAddBlockIdToSection
+ org.asciidoc.intellij.actions.intentions.AsciiDocAddBlockIdToSectionIntention
diff --git a/src/main/resources/intentionDescriptions/AsciiDocAddBlockIdToSection/after.adoc.template b/src/main/resources/intentionDescriptions/AsciiDocAddBlockIdToSectionIntention/after.adoc.template
similarity index 100%
rename from src/main/resources/intentionDescriptions/AsciiDocAddBlockIdToSection/after.adoc.template
rename to src/main/resources/intentionDescriptions/AsciiDocAddBlockIdToSectionIntention/after.adoc.template
diff --git a/src/main/resources/intentionDescriptions/AsciiDocAddBlockIdToSection/before.adoc.template b/src/main/resources/intentionDescriptions/AsciiDocAddBlockIdToSectionIntention/before.adoc.template
similarity index 100%
rename from src/main/resources/intentionDescriptions/AsciiDocAddBlockIdToSection/before.adoc.template
rename to src/main/resources/intentionDescriptions/AsciiDocAddBlockIdToSectionIntention/before.adoc.template
diff --git a/src/main/resources/intentionDescriptions/AsciiDocAddBlockIdToSection/description.html b/src/main/resources/intentionDescriptions/AsciiDocAddBlockIdToSectionIntention/description.html
similarity index 100%
rename from src/main/resources/intentionDescriptions/AsciiDocAddBlockIdToSection/description.html
rename to src/main/resources/intentionDescriptions/AsciiDocAddBlockIdToSectionIntention/description.html
diff --git a/src/test/java/org/asciidoc/intellij/intentions/AsciiDocAddBlockIdToSectionIntentionTest.java b/src/test/java/org/asciidoc/intellij/intentions/AsciiDocAddBlockIdToSectionIntentionTest.java
new file mode 100644
index 000000000..a36126de1
--- /dev/null
+++ b/src/test/java/org/asciidoc/intellij/intentions/AsciiDocAddBlockIdToSectionIntentionTest.java
@@ -0,0 +1,23 @@
+package org.asciidoc.intellij.intentions;
+
+import org.asciidoc.intellij.inspections.AsciiDocQuickFixTestBase;
+
+/**
+ * @author Alexander Schwartz (alexander.schwartz@gmx.net)
+ */
+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";
+ }
+}
diff --git a/testData/intentions/blockIdToSection/subsection-after.adoc b/testData/intentions/blockIdToSection/subsection-after.adoc
new file mode 100644
index 000000000..9819a2cd7
--- /dev/null
+++ b/testData/intentions/blockIdToSection/subsection-after.adoc
@@ -0,0 +1,6 @@
+= Dummy page
+
+[#_dummy_paragraph]
+== Dummy Paragraph
+
+some dummy text
diff --git a/testData/intentions/blockIdToSection/subsection.adoc b/testData/intentions/blockIdToSection/subsection.adoc
new file mode 100644
index 000000000..a9b3fdc32
--- /dev/null
+++ b/testData/intentions/blockIdToSection/subsection.adoc
@@ -0,0 +1,5 @@
+= Dummy page
+
+== Dummy Paragraph
+
+some dummy text