Skip to content

Commit c7e13e1

Browse files
committed
fix toggle actions
1 parent 0262eff commit c7e13e1

2 files changed

Lines changed: 24 additions & 18 deletions

File tree

src/main/kotlin/org/polyfrost/intelliprocessor/action/PreprocessorCommentToggleBlockAction.kt

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,20 @@ class PreprocessorCommentToggleBlockAction : AnAction() {
6363

6464
val lineStart = document.getLineStartOffset(line)
6565
val lineEnd = document.getLineEndOffset(line)
66-
val text = document.getText(TextRange(lineStart, lineEnd))
66+
var text = document.getText(TextRange(lineStart, lineEnd))
67+
68+
if (text.trimStart().startsWith("//#")) {
69+
warning(project, "Action tried to modify preprocessor directive line at line#${line + 1}, aborting.")
70+
break // something is very wrong abort
71+
}
6772

6873
// Clean up any existing toggle comments
69-
document.replaceString(lineStart, lineEnd, text.replaceFirst(REPLACE, ""))
74+
text = text.replaceFirst(REPLACE, "")
7075

71-
if (toggleCommentsOff) continue
76+
if (toggleCommentsOff) {
77+
document.replaceString(lineStart, lineEnd, text)
78+
continue
79+
}
7280

7381
// If the line is blank, just insert a blank comment
7482
if (text.trim().isEmpty()) {

src/main/kotlin/org/polyfrost/intelliprocessor/action/PreprocessorCommentToggleLineAction.kt

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@ import com.intellij.openapi.editor.Editor
77
import com.intellij.openapi.fileEditor.FileEditorManager
88
import com.intellij.openapi.project.Project
99
import com.intellij.openapi.util.TextRange
10-
import org.jetbrains.kotlin.idea.base.psi.getLineNumber
11-
import org.polyfrost.intelliprocessor.utils.PreprocessorContainingBlock
12-
import org.polyfrost.intelliprocessor.utils.activeFile
13-
import org.polyfrost.intelliprocessor.utils.allPreprocessorDirectiveComments
1410

1511
class PreprocessorCommentToggleLineAction : AnAction() {
1612

@@ -19,26 +15,28 @@ class PreprocessorCommentToggleLineAction : AnAction() {
1915
val editor: Editor = FileEditorManager.getInstance(project).selectedTextEditor ?: return warning(project, "Could not find an open editor")
2016
val document = editor.document
2117

22-
val startLine = editor.caretModel.primaryCaret.selectionStartPosition.line
23-
val endLine = editor.caretModel.primaryCaret.selectionEndPosition.line
18+
val startLine = document.getLineNumber(editor.selectionModel.selectionStart)
19+
val endLine = document.getLineNumber(editor.selectionModel.selectionEnd)
2420

2521
WriteCommandAction.runWriteCommandAction(project) {
2622
for (line in startLine..endLine) {
2723
val lineStart = document.getLineStartOffset(line)
2824
val lineEnd = document.getLineEndOffset(line)
2925
val text = document.getText(TextRange(lineStart, lineEnd))
30-
31-
val toggleCommentsOff = text.contains("//$$")
32-
33-
// Clean up any existing toggle comments
34-
document.replaceString(lineStart, lineEnd, text.replaceFirst(REPLACE, ""))
35-
36-
if (toggleCommentsOff) return@runWriteCommandAction
26+
val trim = text.trim()
3727

3828
// If the line is blank, just insert a blank comment
39-
if (text.trim().isEmpty()) {
29+
if (trim.isEmpty()) {
4030
document.replaceString(lineStart, lineEnd, "//$$ ")
41-
return@runWriteCommandAction
31+
continue
32+
}
33+
34+
if (trim.startsWith("//#")) continue
35+
36+
if (trim.startsWith("//$$")) {
37+
// Clean up any existing toggle comments
38+
document.replaceString(lineStart, lineEnd, text.replaceFirst(REPLACE, ""))
39+
continue
4240
}
4341

4442
// Only comment lines that are indented at least as much as the block start

0 commit comments

Comments
 (0)