Skip to content

Commit 0aaca54

Browse files
Support: Toggling Highlight
1 parent 4c11b75 commit 0aaca54

File tree

7 files changed

+139
-19
lines changed

7 files changed

+139
-19
lines changed

CHANGELOG.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,21 @@ All notable changes to the Trailing Spaces plugin will be documented in this fil
55
## [Unreleased]
66
### Added
77
- **Real-time highlighting** of trailing spaces in all open documents
8+
- **Toggle highlighting functionality** to enable/disable trailing space highlighting
9+
- Available via Edit → Trailing Spaces → Toggle Highlighting menu
10+
- Default keyboard shortcut: `Alt+Shift+D`
11+
- Dynamic action text that shows current state (Enable/Disable Highlighting)
12+
- Instantly updates all open editors when toggled
813
- **Delete action** to remove all trailing spaces in the current document
914
- Available via Command Palette (`Ctrl+Shift+A` / `Cmd+Shift+A`) - search "Trailing Spaces: Delete"
15+
- Available via Edit → Trailing Spaces → Delete menu
1016
- Default keyboard shortcut: `Alt+Shift+T`
11-
- Accessible from Edit menu
1217
- **Persistent settings**
1318
- Located at File → Settings → Tools → Trailing Spaces (Windows/Linux)
1419
- Located at Preferences → Tools → Trailing Spaces (Mac)
20+
- **Enable Highlighting setting** (default: enabled)
21+
- Global setting to enable/disable all trailing space highlighting
22+
- Can be toggled via settings panel or toggle action
1523
- **Highlight Current Line setting** (default: enabled)
1624
- When enabled: Always highlight trailing spaces in the currently edited line
1725
- When disabled: Skip highlighting in the current line to avoid distraction while typing

README.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,16 @@ Jetbrains provides a way to automate deletion of trailing spaces in setting. Dep
1111

1212
## Usage
1313

14+
### Toggle Highlighting
15+
16+
You can toggle highlighting of trailing spaces on and off at any time using:
17+
18+
1. **Menu**: Go to **"Edit / Trailing Spaces / Toggle Highlighting"**
19+
2. **Keyboard Shortcut**: Use `Alt+Shift+D` to instantly toggle highlighting
20+
3. **Settings**: Use the "Enable highlighting" checkbox in the plugin settings
21+
22+
The action text dynamically updates to show whether it will "Enable" or "Disable" highlighting based on the current state.
23+
1424
### Delete
1525

1626
The main feature you gain from using this plugin is that of deleting all trailing spaces in the currently edited document. In order to use this deletion feature, you may either:
@@ -19,7 +29,7 @@ The main feature you gain from using this plugin is that of deleting all trailin
1929

2030
2. **Keyboard Shortcut**: The plugin comes with a default keyboard shortcut `Alt+Shift+T` to delete all trailing spaces at once in the current file!
2131

22-
3. **Menu**: Access the action from the "Edit" menu where "Trailing Spaces: Delete" is available
32+
3. **Menu**: Access the action from **"Edit / Trailing Spaces / Delete"**
2333

2434
With these options, you can quickly delete all trailing spaces in your current document whenever needed!
2535

@@ -29,10 +39,15 @@ Several options are available to customize the plugin's behavior. Those settings
2939

3040
To access the settings: Go to **"File / Settings / Tools / Trailing Spaces"** (or **"Preferences / Tools / Trailing Spaces"** on Mac).
3141

42+
### Enable Highlighting
43+
**Default: true**
44+
45+
This is the master toggle for all trailing space highlighting. When disabled, no trailing spaces will be highlighted in any file. This setting can also be toggled using the "Toggle Trailing Spaces" action (`Alt+Shift+D`) or via the menu at "Edit / Trailing Spaces / Toggle Highlighting".
46+
3247
### Highlight Current Line
3348
**Default: true**
3449

3550
Highlighting of trailing spaces in the currently edited line can be annoying: each time you are about to start a new word, the space you type is matched as trailing spaces. The currently edited line can thus be ignored by disabling this option.
3651

37-
When this setting is disabled, trailing spaces in the line being edited will not be highlighted.
52+
When this setting is disabled, trailing spaces in the line being edited will not be highlighted (only effective when "Enable Highlighting" is also enabled).
3853
<!-- Plugin description end -->
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.github.phanthaiduong22.trailingspaces.actions
2+
3+
import com.github.phanthaiduong22.trailingspaces.settings.TrailingSpacesSettings
4+
import com.intellij.openapi.actionSystem.AnAction
5+
import com.intellij.openapi.actionSystem.AnActionEvent
6+
import com.intellij.openapi.actionSystem.CommonDataKeys
7+
import com.intellij.openapi.diagnostic.thisLogger
8+
import com.intellij.openapi.editor.EditorFactory
9+
import com.intellij.openapi.fileEditor.FileEditorManager
10+
import com.intellij.openapi.fileEditor.TextEditor
11+
12+
class ToggleTrailingSpacesAction : AnAction() {
13+
14+
override fun actionPerformed(e: AnActionEvent) {
15+
val project = e.project ?: return
16+
val settings = TrailingSpacesSettings.getInstance()
17+
18+
settings.highlightingEnabled = !settings.highlightingEnabled
19+
20+
thisLogger().info("Trailing spaces highlighting ${if (settings.highlightingEnabled) "enabled" else "disabled"}")
21+
22+
val editors = FileEditorManager.getInstance(project)
23+
.allEditors
24+
.filterIsInstance<TextEditor>()
25+
.map { it.editor }
26+
27+
editors.forEach { editor ->
28+
if (settings.highlightingEnabled) {
29+
editor.project?.let { project ->
30+
val activity = project.getService(com.github.phanthaiduong22.trailingspaces.startup.TrailingSpacesActivity::class.java)
31+
activity?.refreshHighlighting(editor)
32+
}
33+
} else {
34+
clearHighlighters(editor)
35+
}
36+
}
37+
}
38+
39+
override fun update(e: AnActionEvent) {
40+
val editor = e.getData(CommonDataKeys.EDITOR)
41+
e.presentation.isEnabledAndVisible = editor != null
42+
43+
val settings = TrailingSpacesSettings.getInstance()
44+
e.presentation.text = if (settings.highlightingEnabled) {
45+
"Trailing Spaces: Disable Highlighting"
46+
} else {
47+
"Trailing Spaces: Enable Highlighting"
48+
}
49+
}
50+
51+
private fun clearHighlighters(editor: com.intellij.openapi.editor.Editor) {
52+
val markupModel = editor.markupModel
53+
markupModel.allHighlighters.forEach { highlighter ->
54+
if (highlighter.layer == com.github.phanthaiduong22.trailingspaces.config.PluginConfig.HIGHLIGHTER_LAYER) {
55+
markupModel.removeHighlighter(highlighter)
56+
}
57+
}
58+
}
59+
}

src/main/kotlin/com/github/phanthaiduong22/trailingspaces/settings/TrailingSpacesConfigurable.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ class TrailingSpacesConfigurable : Configurable {
2020
override fun createComponent(): JComponent {
2121
val panel = panel {
2222
group("Highlighting Options") {
23+
row {
24+
checkBox("Enable highlighting")
25+
.bindSelected(settings::highlightingEnabled)
26+
.comment("When disabled, no trailing spaces will be highlighted in any file. " +
27+
"You can toggle this setting using the 'Toggle Trailing Spaces' action.")
28+
}
2329
row {
2430
checkBox("Highlight current line")
2531
.bindSelected(settings::highlightCurrentLine)

src/main/kotlin/com/github/phanthaiduong22/trailingspaces/settings/TrailingSpacesSettings.kt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ class TrailingSpacesSettings : PersistentStateComponent<TrailingSpacesSettings.S
2222
private var state = State()
2323

2424
data class State(
25-
var highlightCurrentLine: Boolean = true
25+
var highlightCurrentLine: Boolean = true,
26+
var highlightingEnabled: Boolean = true
2627
)
2728

2829
override fun getState(): State = state
@@ -39,6 +40,15 @@ class TrailingSpacesSettings : PersistentStateComponent<TrailingSpacesSettings.S
3940
set(value) {
4041
state.highlightCurrentLine = value
4142
}
43+
44+
/**
45+
* Whether highlighting is globally enabled
46+
*/
47+
var highlightingEnabled: Boolean
48+
get() = state.highlightingEnabled
49+
set(value) {
50+
state.highlightingEnabled = value
51+
}
4252

4353
companion object {
4454
@JvmStatic

src/main/kotlin/com/github/phanthaiduong22/trailingspaces/startup/TrailingSpacesActivity.kt

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ package com.github.phanthaiduong22.trailingspaces.startup
22

33
import com.github.phanthaiduong22.trailingspaces.config.PluginConfig
44
import com.github.phanthaiduong22.trailingspaces.settings.TrailingSpacesSettings
5+
import com.intellij.openapi.components.Service
56
import com.intellij.openapi.diagnostic.thisLogger
67
import com.intellij.openapi.editor.Editor
78
import com.intellij.openapi.editor.EditorFactory
89
import com.intellij.openapi.editor.event.DocumentEvent
910
import com.intellij.openapi.editor.event.DocumentListener
10-
import com.intellij.openapi.editor.markup.HighlighterLayer
1111
import com.intellij.openapi.editor.markup.HighlighterTargetArea
1212
import com.intellij.openapi.editor.markup.RangeHighlighter
1313
import com.intellij.openapi.editor.markup.TextAttributes
@@ -18,10 +18,10 @@ import com.intellij.openapi.project.Project
1818
import com.intellij.openapi.startup.ProjectActivity
1919
import com.intellij.ui.JBColor
2020
import kotlinx.coroutines.*
21-
import java.awt.Color
2221
import java.util.concurrent.ConcurrentHashMap
2322
import java.util.regex.Pattern
2423

24+
@Service(Service.Level.PROJECT)
2525
class TrailingSpacesActivity : ProjectActivity {
2626
private val trailingSpacePattern = Pattern.compile(PluginConfig.TRAILING_SPACE_PATTERN, PluginConfig.PATTERN_FLAGS)
2727
private val highlighters = mutableMapOf<Editor, MutableList<RangeHighlighter>>()
@@ -87,13 +87,14 @@ class TrailingSpacesActivity : ProjectActivity {
8787
private fun debounceHighlighting(editor: Editor) {
8888
// Cancel existing job for this editor
8989
debounceJobs[editor]?.cancel()
90-
90+
9191
// Start new debounced job
92-
val job = coroutineScope.launch {
93-
delay(PluginConfig.HIGHLIGHTING_DEBOUNCE_DELAY_MS)
94-
highlightTrailingSpaces(editor)
95-
}
96-
92+
val job =
93+
coroutineScope.launch {
94+
delay(PluginConfig.HIGHLIGHTING_DEBOUNCE_DELAY_MS)
95+
highlightTrailingSpaces(editor)
96+
}
97+
9798
debounceJobs[editor] = job
9899
}
99100

@@ -105,6 +106,10 @@ class TrailingSpacesActivity : ProjectActivity {
105106
val markupModel = editor.markupModel
106107
val settings = TrailingSpacesSettings.getInstance()
107108

109+
if (!settings.highlightingEnabled) {
110+
return
111+
}
112+
108113
val caretModel = editor.caretModel
109114
val caretOffset = caretModel.offset
110115
val caretLine = document.getLineNumber(caretOffset)
@@ -143,7 +148,9 @@ class TrailingSpacesActivity : ProjectActivity {
143148

144149
highlighters[editor] = editorHighlighters
145150

146-
thisLogger().debug("Added ${editorHighlighters.size} trailing space highlights (highlightCurrentLine: ${settings.highlightCurrentLine})")
151+
thisLogger().debug(
152+
"Added ${editorHighlighters.size} trailing space highlights (highlightCurrentLine: ${settings.highlightCurrentLine})",
153+
)
147154
}
148155

149156
private fun clearHighlighters(editor: Editor) {
@@ -152,4 +159,8 @@ class TrailingSpacesActivity : ProjectActivity {
152159
}
153160
highlighters.remove(editor)
154161
}
162+
163+
fun refreshHighlighting(editor: Editor) {
164+
highlightTrailingSpaces(editor)
165+
}
155166
}

src/main/resources/META-INF/plugin.xml

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,26 @@
1212
id="com.github.phanthaiduong22.trailingspaces.settings.TrailingSpacesConfigurable"
1313
displayName="Trailing Spaces"/>
1414
<applicationService serviceImplementation="com.github.phanthaiduong22.trailingspaces.settings.TrailingSpacesSettings"/>
15+
<projectService serviceImplementation="com.github.phanthaiduong22.trailingspaces.startup.TrailingSpacesActivity"/>
1516
</extensions>
1617

1718
<actions>
18-
<action id="trailing-spaces.deleteTrailingSpaces"
19-
class="com.github.phanthaiduong22.trailingspaces.actions.DeleteTrailingSpacesAction"
20-
text="Trailing Spaces: Delete"
21-
description="Delete all trailing spaces in the current document">
19+
<group id="TrailingSpacesGroup" text="Trailing Spaces" popup="true">
2220
<add-to-group group-id="EditMenu" anchor="last"/>
23-
<keyboard-shortcut keymap="$default" first-keystroke="alt shift T"/>
24-
</action>
21+
22+
<action id="trailing-spaces.toggleHighlighting"
23+
class="com.github.phanthaiduong22.trailingspaces.actions.ToggleTrailingSpacesAction"
24+
text="Toggle Highlighting"
25+
description="Toggle highlighting of trailing spaces">
26+
<keyboard-shortcut keymap="$default" first-keystroke="alt shift D"/>
27+
</action>
28+
29+
<action id="trailing-spaces.deleteTrailingSpaces"
30+
class="com.github.phanthaiduong22.trailingspaces.actions.DeleteTrailingSpacesAction"
31+
text="Delete"
32+
description="Delete all trailing spaces in the current document">
33+
<keyboard-shortcut keymap="$default" first-keystroke="alt shift T"/>
34+
</action>
35+
</group>
2536
</actions>
2637
</idea-plugin>

0 commit comments

Comments
 (0)