Skip to content

Commit 9ec1b96

Browse files
committed
feat: add fix problem on hover over problem in editor
1 parent b930ef0 commit 9ec1b96

5 files changed

Lines changed: 100 additions & 42 deletions

File tree

src/editor/Editor.js

Lines changed: 44 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1137,26 +1137,63 @@ define(function (require, exports, module) {
11371137
return this._codeMirror.operation(execFn);
11381138
};
11391139

1140-
const MARK_OPTION_UNDERLINE_ERROR = {
1140+
function getMarkOptionUnderlineError() {
1141+
return {
11411142
className: "editor-text-fragment-error"
1142-
}, MARK_OPTION_UNDERLINE_WARN = {
1143+
};
1144+
}
1145+
1146+
function getMarkOptionUnderlineWarn() {
1147+
return {
11431148
className: "editor-text-fragment-warn"
1144-
}, MARK_OPTION_UNDERLINE_INFO = {
1149+
};
1150+
}
1151+
1152+
function getMarkOptionUnderlineInfo() {
1153+
return {
11451154
className: "editor-text-fragment-info"
1146-
}, MARK_OPTION_UNDERLINE_SPELLCHECK = {
1155+
};
1156+
}
1157+
1158+
function getMarkOptionUnderlineSpellcheck() {
1159+
return {
11471160
className: "editor-text-fragment-spell-error"
1148-
}, MARK_OPTION_HYPERLINK_TEXT = {
1161+
};
1162+
}
1163+
1164+
function getMarkOptionHyperlinkText() {
1165+
return {
11491166
className: "editor-text-fragment-hover"
1150-
}, MARK_OPTION_MATCHING_REFS = {
1167+
};
1168+
}
1169+
1170+
function getMarkOptionMatchingRefs() {
1171+
return {
11511172
className: "editor-text-fragment-matching-refs"
1152-
}, MARK_OPTION_RENAME_OUTLINE ={
1173+
};
1174+
}
1175+
1176+
function getMarkOptionRenameOutline() {
1177+
return {
11531178
className: "editor-text-rename-outline",
11541179
startStyle: "editor-text-rename-outline-left",
11551180
endStyle: "editor-text-rename-outline-right",
11561181
clearWhenEmpty: false,
11571182
inclusiveLeft: true,
11581183
inclusiveRight: true
11591184
};
1185+
}
1186+
1187+
/**
1188+
* Mark options to use with API with Editor.markText or Editor.markToken.
1189+
*/
1190+
Editor.getMarkOptionUnderlineError = getMarkOptionUnderlineError;
1191+
Editor.getMarkOptionUnderlineWarn = getMarkOptionUnderlineWarn;
1192+
Editor.getMarkOptionUnderlineInfo = getMarkOptionUnderlineInfo;
1193+
Editor.getMarkOptionUnderlineSpellcheck = getMarkOptionUnderlineSpellcheck;
1194+
Editor.getMarkOptionHyperlinkText = getMarkOptionHyperlinkText;
1195+
Editor.getMarkOptionMatchingRefs = getMarkOptionMatchingRefs;
1196+
Editor.getMarkOptionRenameOutline = getMarkOptionRenameOutline;
11601197

11611198
/**
11621199
* Can be used to mark a range of text with a specific CSS class name. cursorFrom and cursorTo should be {line, ch}
@@ -2591,17 +2628,6 @@ define(function (require, exports, module) {
25912628
Editor.LINE_NUMBER_GUTTER_PRIORITY = LINE_NUMBER_GUTTER_PRIORITY;
25922629
Editor.CODE_FOLDING_GUTTER_PRIORITY = CODE_FOLDING_GUTTER_PRIORITY;
25932630

2594-
/**
2595-
* Mark options to use with API with Editor.markText or Editor.markToken.
2596-
*/
2597-
Editor.MARK_OPTION_UNDERLINE_ERROR = MARK_OPTION_UNDERLINE_ERROR;
2598-
Editor.MARK_OPTION_UNDERLINE_WARN = MARK_OPTION_UNDERLINE_WARN;
2599-
Editor.MARK_OPTION_UNDERLINE_INFO = MARK_OPTION_UNDERLINE_INFO;
2600-
Editor.MARK_OPTION_UNDERLINE_SPELLCHECK = MARK_OPTION_UNDERLINE_SPELLCHECK;
2601-
Editor.MARK_OPTION_HYPERLINK_TEXT = MARK_OPTION_HYPERLINK_TEXT;
2602-
Editor.MARK_OPTION_MATCHING_REFS = MARK_OPTION_MATCHING_REFS;
2603-
Editor.MARK_OPTION_RENAME_OUTLINE = MARK_OPTION_RENAME_OUTLINE;
2604-
26052631
/**
26062632
* Each Editor instance object dispatches the following events:
26072633
* - keydown, keypress, keyup -- When any key event happens in the editor (whether it changes the

src/extensions/default/JavaScriptRefactoring/HighLightReferences.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ define(function (require, exports, module) {
7575
editor.operation(function () {
7676
for(let ref of refsResp.references.refs){
7777
if(editor.document.file.fullPath.endsWith(ref.file)){
78-
editor.markText(HIGHLIGHT_REFS_MARKER, ref.start, ref.end, Editor.MARK_OPTION_MATCHING_REFS);
78+
editor.markText(HIGHLIGHT_REFS_MARKER, ref.start, ref.end, Editor.getMarkOptionMatchingRefs());
7979
}
8080
}
8181
});

src/extensions/default/JavaScriptRefactoring/RenameIdentifier.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ define(function (require, exports, module) {
154154
let selections = currentEditor.getSelections();
155155
if(selections.length > 1 ){
156156
let primary = currentEditor.getSelection();
157-
currentEditor.markText(MARK_TYPE_RENAME, primary.start, primary.end, Editor.MARK_OPTION_RENAME_OUTLINE);
157+
currentEditor.markText(MARK_TYPE_RENAME, primary.start, primary.end, Editor.getMarkOptionRenameOutline());
158158
currentEditor.off(Editor.EVENT_BEFORE_SELECTION_CHANGE + ".renameVar");
159159
currentEditor.off(Editor.EVENT_CURSOR_ACTIVITY + ".renameVar");
160160
currentEditor.off(Editor.EVENT_KEY_DOWN + ".renameVar");

src/features/JumpToDefManager.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ define(function (require, exports, module) {
103103
_clearHoverMarkers(editor);
104104
let jumpToDefProvider = _getJumpToDefProvider(editor, pos);
105105
if(jumpToDefProvider){
106-
editor.markToken(JUMP_TO_DEF_MARKER, pos, Editor.MARK_OPTION_HYPERLINK_TEXT);
106+
editor.markToken(JUMP_TO_DEF_MARKER, pos, Editor.getMarkOptionHyperlinkText());
107107
editor.hoverMarksPresent = true;
108108
}
109109
}

src/language/CodeInspection.js

Lines changed: 53 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -386,9 +386,9 @@ define(function (require, exports, module) {
386386

387387
function _getMarkOptions(error){
388388
switch (error.type) {
389-
case Type.ERROR: return Editor.MARK_OPTION_UNDERLINE_ERROR;
390-
case Type.WARNING: return Editor.MARK_OPTION_UNDERLINE_WARN;
391-
case Type.META: return Editor.MARK_OPTION_UNDERLINE_INFO;
389+
case Type.ERROR: return Editor.getMarkOptionUnderlineError();
390+
case Type.WARNING: return Editor.getMarkOptionUnderlineWarn();
391+
case Type.META: return Editor.getMarkOptionUnderlineInfo();
392392
}
393393
}
394394

@@ -490,18 +490,47 @@ define(function (require, exports, module) {
490490
_populateDummyGutterElements(editor, from, to);
491491
}
492492

493+
function _scrollToTableLine(lineNumber) {
494+
const $lineElement = $problemsPanelTable.find('td.line-number[data-line="' + lineNumber + '"]');
495+
if ($lineElement.length) {
496+
$lineElement[0].scrollIntoView({ behavior: 'instant', block: 'start' });
497+
}
498+
}
499+
500+
493501
function getQuickView(editor, pos, token, line) {
494502
return new Promise((resolve, reject)=>{
495503
let codeInspectionMarks = editor.findMarksAt(pos, CODE_MARK_TYPE_INSPECTOR) || [];
496-
let hoverMessage = '';
504+
let $hoverMessage = $(`<div class="code-inspection-item"></div>`);
505+
let $problemView, quickViewPresent;
497506
for(let mark of codeInspectionMarks){
498-
hoverMessage = `${hoverMessage}${mark.message}<br/>`;
507+
quickViewPresent = true;
508+
const fixID = `${mark.metadata}`;
509+
if(documentFixes.get(fixID)){
510+
$problemView = $(`<div>
511+
<button class="btn btn-mini primary fix-problem-btn" style="margin-right: 5px;">Fix</button>
512+
${mark.message}<br/>
513+
</div>`);
514+
$problemView.find(".fix-problem-btn").click(()=>{
515+
_scrollToTableLine(pos.line);
516+
_fixProblem(fixID);
517+
});
518+
$hoverMessage.append($problemView);
519+
} else {
520+
$problemView = $(`<div>${mark.message}<br/></div>`);
521+
$hoverMessage.append($problemView);
522+
}
523+
// eslint-disable-next-line no-loop-func
524+
$problemView.click(function () {
525+
toggleCollapsed(false);
526+
_scrollToTableLine(pos.line);
527+
});
499528
}
500-
if(hoverMessage){
529+
if(quickViewPresent){
501530
resolve({
502-
start: {line: pos.line, ch: token.start},
531+
start: {line: pos.line, ch: token.start}, // tod this should not be of the token
503532
end: {line: pos.line, ch: token.end},
504-
content: `<div class="code-inspection-item">${hoverMessage}</div>`
533+
content: $hoverMessage
505534
});
506535
return;
507536
}
@@ -941,6 +970,21 @@ define(function (require, exports, module) {
941970
description: Strings.DESCRIPTION_USE_PREFERED_ONLY
942971
});
943972

973+
function _fixProblem(fixID) {
974+
const fixDetails = documentFixes.get(fixID);
975+
const editor = EditorManager.getCurrentFullEditor();
976+
if(!editor || !fixDetails || editor.document.lastChangeTimestamp !== lastDocumentScanTimeStamp) {
977+
Dialogs.showErrorDialog(Strings.CANNOT_FIX_TITLE, Strings.CANNOT_FIX_MESSAGE);
978+
} else {
979+
const from = editor.posFromIndex(fixDetails.rangeOffset.start),
980+
to = editor.posFromIndex(fixDetails.rangeOffset.end);
981+
editor.setSelection(from, to, true, Editor.BOUNDARY_BULLSEYE, EDIT_ORIGIN_LINT_FIX);
982+
editor.replaceSelection(fixDetails.replaceText, "around");
983+
}
984+
MainViewManager.focusActivePane();
985+
run();
986+
}
987+
944988
// Initialize items dependent on HTML DOM
945989
AppInit.htmlReady(function () {
946990
Editor.registerGutter(CODE_INSPECTION_GUTTER, CODE_INSPECTION_GUTTER_PRIORITY);
@@ -974,21 +1018,9 @@ define(function (require, exports, module) {
9741018
}
9751019
if ($(e.target).hasClass('ph-fix-problem')) {
9761020
// Retrieve the message from the data attribute of the clicked element
977-
const fixid = "" + $(e.target).data("fixid");
978-
const fixDetails = documentFixes.get(fixid);
979-
const editor = EditorManager.getCurrentFullEditor();
980-
if(!editor || !fixDetails || editor.document.lastChangeTimestamp !== lastDocumentScanTimeStamp) {
981-
Dialogs.showErrorDialog(Strings.CANNOT_FIX_TITLE, Strings.CANNOT_FIX_MESSAGE);
982-
} else {
983-
const from = editor.posFromIndex(fixDetails.rangeOffset.start),
984-
to = editor.posFromIndex(fixDetails.rangeOffset.end);
985-
editor.setSelection(from, to, true, Editor.BOUNDARY_BULLSEYE, EDIT_ORIGIN_LINT_FIX);
986-
editor.replaceSelection(fixDetails.replaceText, "around");
987-
}
1021+
_fixProblem("" + $(e.target).data("fixid"));
9881022
e.preventDefault();
9891023
e.stopPropagation();
990-
MainViewManager.focusActivePane();
991-
run();
9921024
return;
9931025
}
9941026

0 commit comments

Comments
 (0)