Skip to content

Commit 040196e

Browse files
committed
chore: performance improveement for scrolling and indent guides
1 parent 07244b4 commit 040196e

File tree

4 files changed

+86
-57
lines changed

4 files changed

+86
-57
lines changed

src/editor/Editor.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ define(function (require, exports, module) {
445445
const lineHeight = parseFloat(getComputedStyle($editor[0]).lineHeight);
446446
const scrollDelta = event.deltaY;
447447
const defaultHeight = 14, scrollScaleFactor = lineHeight/defaultHeight;
448-
$editor[0].scrollTop += (scrollDelta/scrollScaleFactor/2);
448+
$editor[0].scrollTop += (scrollDelta/scrollScaleFactor);
449449
event.preventDefault();
450450
});
451451
}

src/extensionsIntegrated/indentGuides/main.js

Lines changed: 73 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ define(function (require, exports, module) {
3636

3737
const COMMAND_NAME = Strings.CMD_TOGGLE_INDENT_GUIDES,
3838
COMMAND_ID = Commands.TOGGLE_INDENT_GUIDES,
39-
GUIDE_CLASS = "phcode-indent-guides";
39+
GUIDE_CLASS = "phcode-indent-guides",
40+
GUIDE_CLASS_NONE = "phcode-indent-guides-none";
4041

4142
const PREFERENCES_EDITOR_INDENT_GUIDES = "editor.indentGuides",
4243
PREFERENCES_EDITOR_INDENT_HIDE_FIRST = "editor.indentHideFirst";
@@ -54,69 +55,89 @@ define(function (require, exports, module) {
5455
});
5556

5657
// 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-
}
58+
function _createOverlayObject(spaceUnits) {
59+
return {
60+
_spaceUnits: spaceUnits,
61+
_cmRefreshPending: false,
62+
token: function (stream, _state) {
63+
let char = "",
64+
colNum = 0,
65+
isTabStart = false;
66+
67+
char = stream.next();
68+
colNum = stream.column();
69+
70+
// Check for "hide first guide" preference
71+
if ((hideFirst) && (colNum === 0)) {
72+
return null;
73+
}
7174

72-
if (char === "\t") {
73-
return GUIDE_CLASS;
74-
}
75+
if (char === "\t") {
76+
return enabled? GUIDE_CLASS : GUIDE_CLASS_NONE;
77+
}
7578

76-
if (char !== " ") {
77-
stream.skipToEnd();
78-
return null;
79-
}
79+
if (char !== " ") {
80+
stream.skipToEnd();
81+
return null;
82+
}
8083

81-
spaceUnits = Editor.getSpaceUnits();
82-
isTabStart = (colNum % spaceUnits) ? false : true;
84+
isTabStart = (colNum % this._spaceUnits) === 0 ? true : false;
8385

84-
if ((char === " ") && (isTabStart)) {
85-
return GUIDE_CLASS;
86-
}
87-
return null;
88-
},
89-
flattenSpans: false
90-
};
86+
if ((char === " ") && (isTabStart)) {
87+
return enabled? GUIDE_CLASS : GUIDE_CLASS_NONE;
88+
}
89+
return null;
90+
},
91+
flattenSpans: false
92+
};
93+
}
9194

9295
function applyPreferences() {
9396
enabled = PreferencesManager.get(PREFERENCES_EDITOR_INDENT_GUIDES);
9497
hideFirst = PreferencesManager.get(PREFERENCES_EDITOR_INDENT_HIDE_FIRST);
9598
}
9699

97100
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();
101+
const editor = EditorManager.getActiveEditor();
102+
if(!editor || !editor._codeMirror) {
103+
return;
104+
}
105+
const cm = editor._codeMirror;
106+
if(!editor.__indentGuidesOverlay) {
107+
editor.__indentGuidesOverlay = _createOverlayObject(Editor.getSpaceUnits(editor.document.file.fullPath));
108+
}
109+
function _reRenderOverlay() {
110+
cm.removeOverlay(editor.__indentGuidesOverlay);
111+
cm.addOverlay(editor.__indentGuidesOverlay);
112+
}
113+
editor.off(Editor.EVENT_OPTION_CHANGE+".indentGuide");
114+
editor.on(Editor.EVENT_OPTION_CHANGE+".indentGuide", ()=>{
115+
const newSpaceUnits = Editor.getSpaceUnits(editor.document.file.fullPath);
116+
if(newSpaceUnits !== editor.__indentGuidesOverlay._spaceUnits){
117+
editor.__indentGuidesOverlay._spaceUnits = newSpaceUnits;
118+
if(EditorManager.getActiveEditor() === editor){
119+
console.log("space units changed, refreshing indent guides for",
120+
newSpaceUnits, editor.document.file.fullPath);
121+
_reRenderOverlay();
108122
}
109-
} else if(enabled){
110-
cm.removeOverlay(indentGuidesOverlay);
111-
cm.addOverlay(indentGuidesOverlay);
112-
editor._overlayPresent = true;
113-
cm.refresh();
114123
}
124+
});
125+
126+
let shouldRerender = false;
127+
if(!cm.__indentGuidesOverlayAttached){
128+
cm.addOverlay(editor.__indentGuidesOverlay);
129+
cm.__indentGuidesOverlayAttached = editor.__indentGuidesOverlay;
130+
cm.__overlayEnabled = enabled;
131+
} else if(cm.__indentGuidesOverlayAttached &&
132+
cm.__indentGuidesOverlayAttached !== editor.__indentGuidesOverlay) {
133+
cm.removeOverlay(cm.__indentGuidesOverlayAttached);
134+
cm.addOverlay(editor.__indentGuidesOverlay);
135+
cm.__overlayEnabled = enabled;
136+
} else if (shouldRerender || cm.__overlayEnabled !== enabled) {
137+
cm.__overlayEnabled = enabled;
138+
_reRenderOverlay();
139+
console.log("Refreshing indent guides");
115140
}
116-
117-
// Update menu
118-
CommandManager.get(COMMAND_ID)
119-
.setChecked(enabled);
120141
}
121142

122143
function handleToggleGuides() {
@@ -127,6 +148,8 @@ define(function (require, exports, module) {
127148
function preferenceChanged() {
128149
applyPreferences();
129150
updateUI();
151+
CommandManager.get(COMMAND_ID)
152+
.setChecked(enabled);
130153
}
131154

132155
// Initialize extension

src/styles/brackets_codemirror_override.less

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -289,9 +289,18 @@ span.cm-emstrong {
289289
font-weight: normal;
290290
}
291291

292-
.cm-phcode-indent-guides {
292+
.cm-phcode-indent-guides::before {
293+
content: " ";
294+
width: 1px;
295+
display: inline-block;
293296
position: relative;
294-
background-repeat: repeat-y;
295-
background-image: url('images/indent-guide-line.svg');
297+
border-left: 1px solid rgba(128, 128, 128, 0.3);
298+
}
299+
300+
.cm-phcode-indent-guides-none::before {
301+
content: " ";
302+
width: 1px;
296303
display: inline-block;
304+
position: relative;
305+
border-left: 1px solid transparent;
297306
}

src/styles/images/indent-guide-line.svg

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)