Skip to content

Commit 1068d17

Browse files
committed
feat: indent guides
1 parent 24b5640 commit 1068d17

File tree

7 files changed

+165
-2
lines changed

7 files changed

+165
-2
lines changed

src/command/Commands.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ define(function (require, exports, module) {
129129
exports.TOGGLE_ACTIVE_LINE = "view.toggleActiveLine"; // EditorOptionHandlers.js _getToggler()
130130
exports.TOGGLE_WORD_WRAP = "view.toggleWordWrap"; // EditorOptionHandlers.js _getToggler()
131131
exports.TOGGLE_RULERS = "view.toggleRulers"; // EditorOptionHandlers.js
132+
exports.TOGGLE_INDENT_GUIDES = "view.toggleIndentGuides"; // integrated extension indentGuides
132133
exports.TOGGLE_SEARCH_AUTOHIDE = "view.toggleSearchAutoHide"; // EditorOptionHandlers.js _getToggler()
133134

134135
exports.CMD_OPEN = "cmd.open";
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
/*
2+
* GNU AGPL-3.0 License
3+
*
4+
* Copyright (c) 2021 - present core.ai . All rights reserved.
5+
*
6+
* This program is free software: you can redistribute it and/or modify it
7+
* under the terms of the GNU Affero General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
14+
* for more details.
15+
*
16+
* You should have received a copy of the GNU Affero General Public License
17+
* along with this program. If not, see https://opensource.org/licenses/AGPL-3.0.
18+
*
19+
*/
20+
21+
// Adapted from https://github.com/lkcampbell/brackets-indent-guides by Lance Campbell.
22+
23+
/*jslint vars: true, plusplus: true, devel: true, regexp: true, nomen: true, indent: 4, maxerr: 50 */
24+
25+
define(function (require, exports, module) {
26+
27+
const PreferencesManager = require("preferences/PreferencesManager"),
28+
Menus = require("command/Menus"),
29+
Editor = require("editor/Editor").Editor,
30+
EditorManager = require("editor/EditorManager"),
31+
AppInit = require("utils/AppInit"),
32+
Commands = require("command/Commands"),
33+
CommandManager = require("command/CommandManager"),
34+
MainViewManager = require("view/MainViewManager"),
35+
Strings = require("strings");
36+
37+
const COMMAND_NAME = Strings.CMD_TOGGLE_INDENT_GUIDES,
38+
COMMAND_ID = Commands.TOGGLE_INDENT_GUIDES,
39+
GUIDE_CLASS = "phcode-indent-guides";
40+
41+
const PREFERENCES_EDITOR_INDENT_GUIDES = "editor.indentGuides",
42+
PREFERENCES_EDITOR_INDENT_HIDE_FIRST = "editor.indentHideFirst";
43+
44+
// Define extension preferences
45+
let enabled = true,
46+
hideFirst = false;
47+
48+
PreferencesManager.definePreference(PREFERENCES_EDITOR_INDENT_GUIDES, "boolean", enabled, {
49+
description: Strings.DESCRIPTION_INDENT_GUIDES_ENABLED
50+
});
51+
52+
PreferencesManager.definePreference(PREFERENCES_EDITOR_INDENT_HIDE_FIRST, "boolean", hideFirst, {
53+
description: Strings.DESCRIPTION_HIDE_FIRST
54+
});
55+
56+
// CodeMirror overlay code
57+
const indentGuidesOverlay = {
58+
token: function (stream, _state) {
59+
let char = "",
60+
colNum = 0,
61+
spaceUnits = 0,
62+
isTabStart = false;
63+
64+
char = stream.next();
65+
colNum = stream.column();
66+
67+
// Check for "hide first guide" preference
68+
if ((hideFirst) && (colNum === 0)) {
69+
return null;
70+
}
71+
72+
if (char === "\t") {
73+
return GUIDE_CLASS;
74+
}
75+
76+
if (char !== " ") {
77+
stream.skipToEnd();
78+
return null;
79+
}
80+
81+
spaceUnits = Editor.getSpaceUnits();
82+
isTabStart = (colNum % spaceUnits) ? false : true;
83+
84+
if ((char === " ") && (isTabStart)) {
85+
return GUIDE_CLASS;
86+
}
87+
return null;
88+
},
89+
flattenSpans: false
90+
};
91+
92+
function applyPreferences() {
93+
enabled = PreferencesManager.get(PREFERENCES_EDITOR_INDENT_GUIDES);
94+
hideFirst = PreferencesManager.get(PREFERENCES_EDITOR_INDENT_HIDE_FIRST);
95+
}
96+
97+
function updateUI() {
98+
const editor = EditorManager.getActiveEditor(),
99+
cm = editor ? editor._codeMirror : null;
100+
101+
// Update CodeMirror overlay if editor is available
102+
if (cm) {
103+
if(editor._overlayPresent){
104+
if(!enabled){
105+
cm.removeOverlay(indentGuidesOverlay);
106+
editor._overlayPresent = false;
107+
cm.refresh();
108+
}
109+
} else if(enabled){
110+
cm.removeOverlay(indentGuidesOverlay);
111+
cm.addOverlay(indentGuidesOverlay);
112+
editor._overlayPresent = true;
113+
cm.refresh();
114+
}
115+
}
116+
117+
// Update menu
118+
CommandManager.get(COMMAND_ID)
119+
.setChecked(enabled);
120+
}
121+
122+
function handleToggleGuides() {
123+
enabled = !enabled;
124+
PreferencesManager.set(PREFERENCES_EDITOR_INDENT_GUIDES, enabled);
125+
}
126+
127+
function preferenceChanged() {
128+
applyPreferences();
129+
updateUI();
130+
}
131+
132+
// Initialize extension
133+
AppInit.appReady(function () {
134+
// Register command and add to menu
135+
CommandManager.register(COMMAND_NAME, COMMAND_ID, handleToggleGuides);
136+
Menus.getMenu(Menus.AppMenuBar.VIEW_MENU)
137+
.addMenuItem(COMMAND_ID, "", Menus.AFTER, Commands.TOGGLE_RULERS);
138+
139+
// Set up event listeners
140+
PreferencesManager.on("change", PREFERENCES_EDITOR_INDENT_GUIDES, preferenceChanged);
141+
PreferencesManager.on("change", PREFERENCES_EDITOR_INDENT_HIDE_FIRST, preferenceChanged);
142+
143+
MainViewManager.on("currentFileChange", updateUI);
144+
EditorManager.on("activeEditorChange", updateUI);
145+
146+
// Apply preferences and draw indent guides
147+
preferenceChanged();
148+
});
149+
});

src/extensionsIntegrated/loader.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,5 @@ define(function (require, exports, module) {
4141
require("./DisplayShortcuts/main");
4242
require("./appUpdater/main");
4343
require("./HtmlTagSyncEdit/main");
44+
require("./indentGuides/main");
4445
});

src/nls/root/strings.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,7 @@ define({
564564
"CMD_THEMES": "Themes\u2026",
565565
"CMD_TOGGLE_SEARCH_AUTOHIDE": "Automatically close search",
566566
"CMD_TOGGLE_RULERS": "Rulers",
567+
"CMD_TOGGLE_INDENT_GUIDES": "Indent Guide Lines",
567568
"CMD_KEYBOARD_NAV_OVERLAY": "Visual Command Palette",
568569

569570
// Navigate menu commands
@@ -1236,5 +1237,8 @@ define({
12361237
"BEAUTIFY_OPTION_BRACKET_SAME_LINE": "Put the > of a multi-line HTML (HTML, JSX, Vue, Angular) element at the end of the last line instead of being alone on the next line (does not apply to self closing elements)",
12371238
"BEAUTIFY_OPTION_SINGLE_ATTRIBUTE_PER_LINE": "Enforce single attribute per line in HTML, Vue and JSX",
12381239
"BEAUTIFY_OPTION_PROSE_WRAP": "Wrap prose if it exceeds the print width in markdown files",
1239-
"BEAUTIFY_OPTION_PRINT_TRAILING_COMMAS": "Print trailing commas wherever possible in multi-line comma-separated syntactic structures"
1240+
"BEAUTIFY_OPTION_PRINT_TRAILING_COMMAS": "Print trailing commas wherever possible in multi-line comma-separated syntactic structures",
1241+
// indent guides extension
1242+
"DESCRIPTION_INDENT_GUIDES_ENABLED": "true to show indent guide lines, else false.",
1243+
"DESCRIPTION_HIDE_FIRST": "true to show the first Indent Guide line else false."
12401244
});

src/preferences/StateManager.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
* GNU AGPL-3.0 License
33
*
44
* Copyright (c) 2021 - present core.ai . All rights reserved.
5-
* Original work Copyright (c) 2012 - 2021 Adobe Systems Incorporated. All rights reserved.
65
*
76
* This program is free software: you can redistribute it and/or modify it
87
* under the terms of the GNU Affero General Public License as published by

src/styles/brackets_codemirror_override.less

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,3 +288,9 @@ span.cm-emstrong {
288288
font-style: normal;
289289
font-weight: normal;
290290
}
291+
292+
.cm-phcode-indent-guides {
293+
position: relative;
294+
background-repeat: repeat-y;
295+
background-image: url('images/indent-guide-line.svg');
296+
}
Lines changed: 3 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)