-
Notifications
You must be signed in to change notification settings - Fork 162
Expand file tree
/
Copy pathInspection.ts
More file actions
69 lines (65 loc) · 2.45 KB
/
Copy pathInspection.ts
File metadata and controls
69 lines (65 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import { TextDocument, workspace, window, Selection, Range, Position } from "vscode";
import { SymbolNode } from "./SymbolNode";
export interface InspectionProblem {
/**
* short description of the problem
*/
description: string;
position: {
/**
* real line number to the start of the document, will change
*/
line: number;
/**
* relative line number to the start of the symbol(method/class), won't change
*/
relativeLine: number;
/**
* code of the first line of the problematic code block
*/
code: string;
};
/**
* indicator of the problematic code block, e.g. method name/class name, keywork, etc.
*/
indicator: string;
}
export interface Inspection {
id: string;
document?: TextDocument;
symbol?: SymbolNode;
problem: InspectionProblem;
solution: string;
severity: string;
}
export namespace Inspection {
export function revealFirstLineOfInspection(inspection: Inspection) {
inspection.document && void workspace.openTextDocument(inspection.document.uri).then(document => {
void window.showTextDocument(document).then(editor => {
const range = getIndicatorRangeOfInspection(inspection.problem);
editor.selection = new Selection(range.start, range.end);
editor.revealRange(range);
});
});
}
/**
* get the range of the indicator of the inspection.
* `indicator` will be used as the position of code lens/diagnostics and also used as initial selection for fix commands.
*/
export function getIndicatorRangeOfInspection(problem: InspectionProblem): Range {
const position = problem.position;
const startLine: number = position.line;
let startColumn: number = position.code.indexOf(problem.indicator), endLine: number = -1, endColumn: number = -1;
if (startColumn > -1) {
// highlight only the symbol
endLine = position.line;
endColumn = startColumn + problem.indicator?.length;
} else {
// highlight entire first line
startColumn = position.code.search(/\S/) ?? 0; // first non-whitespace character
endLine = position.line;
endColumn = position.code.length; // last character
}
return new Range(new Position(startLine, startColumn), new Position(endLine, endColumn));
}
}