Skip to content

Commit

Permalink
Inactive region highlight (#1505)
Browse files Browse the repository at this point in the history
* Add intermediary type to safely construct vscode.Ranges for use

* Fix incorrect object definition

* Fix file empty edge case; refactor + comment out code while testing

* Use InputRegions (lines only); Remove commented out code

* Commenting

* Fix linter error from merge with master
  • Loading branch information
grdowns authored Jan 31, 2018
1 parent 42ddf18 commit fd7b691
Showing 1 changed file with 17 additions and 33 deletions.
50 changes: 17 additions & 33 deletions Extension/src/LanguageServer/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,12 @@ interface OutputNotificationBody {

interface InactiveRegionParams {
uri: string;
ranges: InputRange[];
regions: InputRegion[];
}

interface InputRange {
start: { line: number; character: number };
end: { line: number; character: number };
interface InputRegion {
startLine: number;
endLine: number;
}

interface DecorationRangesPair {
Expand Down Expand Up @@ -137,8 +137,9 @@ function collectSettingsForTelemetry(filter: (key: string, val: string, settings
let curSetting: any = util.packageJson.contributes.configuration.properties["C_Cpp." + key];
if (curSetting) {
let curEnum: any[] = curSetting["enum"];
if (curEnum && curEnum.indexOf(val) === -1)
if (curEnum && curEnum.indexOf(val) === -1) {
continue;
}
}

if (filter(key, val, settings)) {
Expand Down Expand Up @@ -699,24 +700,22 @@ class DefaultClient implements Client {
};
let decoration: vscode.TextEditorDecorationType = vscode.window.createTextEditorDecorationType(renderOptions);

// As InactiveRegionParams.ranges is deserialized as POD, we must convert to vscode.Ranges in order to make use of the API's
// We must convert to vscode.Ranges in order to make use of the API's
let ranges: vscode.Range[] = [];
params.ranges.forEach(element => {
ranges.push(new vscode.Range(element.start.line, element.start.character, element.end.line, element.end.character));
params.regions.forEach(element => {
let newRange : vscode.Range = new vscode.Range(element.startLine, 0, element.endLine, 0);
ranges.push(newRange);
});

// Recycle the active text decorations when we receive a new set of inactive regions
// Find entry for cached file and act accordingly
let valuePair: DecorationRangesPair = this.inactiveRegionsDecorations.get(params.uri);
if (valuePair) {
// The language server will send notifications regardless of whether the ranges have changed, so we must check to see if the new data reflects a change
if (!this.areRangesEqual(valuePair.ranges, ranges)) {
// Disposing of and resetting the decoration will undo previously applied text decorations
valuePair.decoration.dispose();
valuePair.decoration = decoration;

// As vscode.TextEditor.setDecorations only applies to visible editors, we must cache the range for when another editor becomes visible
valuePair.ranges = ranges;
}
// Disposing of and resetting the decoration will undo previously applied text decorations
valuePair.decoration.dispose();
valuePair.decoration = decoration;

// As vscode.TextEditor.setDecorations only applies to visible editors, we must cache the range for when another editor becomes visible
valuePair.ranges = ranges;
} else { // The entry does not exist. Make a new one
let toInsert: DecorationRangesPair = {
decoration: decoration,
Expand All @@ -732,21 +731,6 @@ class DefaultClient implements Client {
}
}

// Helper method to compare two ranges arrays for equality
private areRangesEqual(r1: vscode.Range[], r2: vscode.Range[]): boolean {
if (r1.length !== r2.length) {
return false;
}

for (let i: number = 0; i < r1.length; ++i) {
if (!r1[i].isEqual(r2[i])) {
return false;
}
}

return true;
}

/*********************************************
* requests to the language server
*********************************************/
Expand Down

0 comments on commit fd7b691

Please sign in to comment.