-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
markers: add sorting of problem markers
Fixes #6572 The following commit updates the `markers` to be sorted based on the following rules: - markers are sorted by `severity`. - if the first step is equal, markers are sorted by `line number`. - if the second step is equal, markers are sorted by `column number`. - else return no change. Signed-off-by: Vincent Fugnitto <vincent.fugnitto@ericsson.com>
- Loading branch information
1 parent
d4d607b
commit 9b7476b
Showing
4 changed files
with
265 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
176 changes: 176 additions & 0 deletions
176
packages/markers/src/browser/problem/problem-tree-model.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
/******************************************************************************** | ||
* Copyright (C) 2020 Ericsson and others. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License v. 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0. | ||
* | ||
* This Source Code may also be made available under the following Secondary | ||
* Licenses when the conditions for such availability set forth in the Eclipse | ||
* Public License v. 2.0 are satisfied: GNU General Public License, version 2 | ||
* with the GNU Classpath Exception which is available at | ||
* https://www.gnu.org/software/classpath/license.html. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 | ||
********************************************************************************/ | ||
|
||
import { enableJSDOM } from '@theia/core/lib/browser/test/jsdom'; | ||
let disableJSDOM = enableJSDOM(); | ||
|
||
import URI from '@theia/core/lib/common/uri'; | ||
import { expect } from 'chai'; | ||
import { Container } from 'inversify'; | ||
import { Diagnostic, Range, DiagnosticSeverity } from 'vscode-languageserver-types'; | ||
import { Event } from '@theia/core/lib/common/event'; | ||
import { FileSystemWatcher } from '@theia/filesystem/lib/browser/filesystem-watcher'; | ||
import { Marker } from '../../common/marker'; | ||
import { MarkerManager } from '../marker-manager'; | ||
import { MarkerNode, MarkerOptions } from '../marker-tree'; | ||
import { PROBLEM_OPTIONS } from './problem-container'; | ||
import { ProblemManager } from './problem-manager'; | ||
import { ProblemTree } from './problem-tree-model'; | ||
|
||
disableJSDOM(); | ||
|
||
let problemTree: ProblemTree; | ||
|
||
before(() => { | ||
disableJSDOM = enableJSDOM(); | ||
const testContainer = new Container(); | ||
|
||
testContainer.bind(MarkerManager).toSelf().inSingletonScope(); | ||
testContainer.bind(ProblemManager).toSelf(); | ||
testContainer.bind(MarkerOptions).toConstantValue(PROBLEM_OPTIONS); | ||
testContainer.bind(FileSystemWatcher).toConstantValue({ | ||
onFilesChanged: Event.None | ||
} as FileSystemWatcher); | ||
|
||
testContainer.bind(ProblemTree).toSelf().inSingletonScope(); | ||
problemTree = testContainer.get<ProblemTree>(ProblemTree); | ||
}); | ||
|
||
after(() => { | ||
disableJSDOM(); | ||
}); | ||
|
||
describe('Problem Tree', () => { | ||
|
||
describe('#sortMarkers', () => { | ||
describe('should sort markers based on the highest severity', () => { | ||
it('should sort errors higher than warnings', () => { | ||
const markerA = createMockMarker({ start: { line: 0, character: 10 }, end: { line: 0, character: 10 } }, DiagnosticSeverity.Error); | ||
const markerB = createMockMarker({ start: { line: 0, character: 10 }, end: { line: 0, character: 10 } }, DiagnosticSeverity.Warning); | ||
const nodeA = createMockMarkerNode(markerA); | ||
const nodeB = createMockMarkerNode(markerB); | ||
expect(problemTree['sortMarkers'](nodeA, nodeB)).equals(-1); | ||
expect(problemTree['sortMarkers'](nodeB, nodeA)).equals(1); | ||
}); | ||
it('should sort errors higher than infos', () => { | ||
const markerA = createMockMarker({ start: { line: 0, character: 10 }, end: { line: 0, character: 10 } }, DiagnosticSeverity.Error); | ||
const markerB = createMockMarker({ start: { line: 0, character: 10 }, end: { line: 0, character: 10 } }, DiagnosticSeverity.Information); | ||
const nodeA = createMockMarkerNode(markerA); | ||
const nodeB = createMockMarkerNode(markerB); | ||
expect(problemTree['sortMarkers'](nodeA, nodeB)).equals(-2); | ||
expect(problemTree['sortMarkers'](nodeB, nodeA)).equals(2); | ||
}); | ||
it('should sort errors higher than hints', () => { | ||
const markerA = createMockMarker({ start: { line: 0, character: 10 }, end: { line: 0, character: 10 } }, DiagnosticSeverity.Error); | ||
const markerB = createMockMarker({ start: { line: 0, character: 10 }, end: { line: 0, character: 10 } }, DiagnosticSeverity.Hint); | ||
const nodeA = createMockMarkerNode(markerA); | ||
const nodeB = createMockMarkerNode(markerB); | ||
expect(problemTree['sortMarkers'](nodeA, nodeB)).equals(-3); | ||
expect(problemTree['sortMarkers'](nodeB, nodeA)).equals(3); | ||
}); | ||
it('should sort warnings higher than infos', () => { | ||
const markerA = createMockMarker({ start: { line: 0, character: 10 }, end: { line: 0, character: 10 } }, DiagnosticSeverity.Warning); | ||
const markerB = createMockMarker({ start: { line: 0, character: 10 }, end: { line: 0, character: 10 } }, DiagnosticSeverity.Information); | ||
const nodeA = createMockMarkerNode(markerA); | ||
const nodeB = createMockMarkerNode(markerB); | ||
expect(problemTree['sortMarkers'](nodeA, nodeB)).equals(-1); | ||
expect(problemTree['sortMarkers'](nodeB, nodeA)).equals(1); | ||
}); | ||
it('should sort warnings higher than hints', () => { | ||
const markerA = createMockMarker({ start: { line: 0, character: 10 }, end: { line: 0, character: 10 } }, DiagnosticSeverity.Warning); | ||
const markerB = createMockMarker({ start: { line: 0, character: 10 }, end: { line: 0, character: 10 } }, DiagnosticSeverity.Hint); | ||
const nodeA = createMockMarkerNode(markerA); | ||
const nodeB = createMockMarkerNode(markerB); | ||
expect(problemTree['sortMarkers'](nodeA, nodeB)).equals(-2); | ||
expect(problemTree['sortMarkers'](nodeB, nodeA)).equals(2); | ||
}); | ||
it('should sort infos higher than hints', () => { | ||
const markerA = createMockMarker({ start: { line: 0, character: 10 }, end: { line: 0, character: 10 } }, DiagnosticSeverity.Information); | ||
const markerB = createMockMarker({ start: { line: 0, character: 10 }, end: { line: 0, character: 10 } }, DiagnosticSeverity.Hint); | ||
const nodeA = createMockMarkerNode(markerA); | ||
const nodeB = createMockMarkerNode(markerB); | ||
expect(problemTree['sortMarkers'](nodeA, nodeB)).equals(-1); | ||
expect(problemTree['sortMarkers'](nodeB, nodeA)).equals(1); | ||
}); | ||
}); | ||
|
||
it('should sort markers based on lowest line number if their severities are equal', () => { | ||
const markerA = createMockMarker({ start: { line: 1, character: 10 }, end: { line: 1, character: 20 } }, DiagnosticSeverity.Error); | ||
const markerB = createMockMarker({ start: { line: 5, character: 10 }, end: { line: 5, character: 20 } }, DiagnosticSeverity.Error); | ||
const nodeA = createMockMarkerNode(markerA); | ||
const nodeB = createMockMarkerNode(markerB); | ||
expect(problemTree['sortMarkers'](nodeA, nodeB)).equals(-4); | ||
expect(problemTree['sortMarkers'](nodeB, nodeA)).equals(4); | ||
}); | ||
|
||
it('should sort markers based on lowest column number if their severities and line numbers are equal', () => { | ||
const markerA = createMockMarker({ start: { line: 1, character: 10 }, end: { line: 1, character: 10 } }, DiagnosticSeverity.Error); | ||
const markerB = createMockMarker({ start: { line: 1, character: 20 }, end: { line: 1, character: 20 } }, DiagnosticSeverity.Error); | ||
const nodeA = createMockMarkerNode(markerA); | ||
const nodeB = createMockMarkerNode(markerB); | ||
expect(problemTree['sortMarkers'](nodeA, nodeB)).equals(-10); | ||
expect(problemTree['sortMarkers'](nodeB, nodeA)).equals(10); | ||
}); | ||
|
||
it('should not sort if markers are equal', () => { | ||
const markerA = createMockMarker({ start: { line: 0, character: 10 }, end: { line: 0, character: 10 } }, DiagnosticSeverity.Error); | ||
const markerB = createMockMarker({ start: { line: 0, character: 10 }, end: { line: 0, character: 10 } }, DiagnosticSeverity.Error); | ||
const nodeA = createMockMarkerNode(markerA); | ||
const nodeB = createMockMarkerNode(markerB); | ||
expect(problemTree['sortMarkers'](nodeA, nodeB)).equals(0); | ||
expect(problemTree['sortMarkers'](nodeB, nodeA)).equals(0); | ||
}); | ||
}); | ||
|
||
}); | ||
|
||
/** | ||
* Create a mock marker node with the given diagnostic marker. | ||
* @param marker the diagnostic marker. | ||
* | ||
* @returns a mock marker node. | ||
*/ | ||
function createMockMarkerNode(marker: Marker<Diagnostic>): MarkerNode { | ||
return { | ||
id: 'id', | ||
name: 'marker', | ||
parent: undefined, | ||
selected: false, | ||
uri: new URI(''), | ||
marker | ||
}; | ||
} | ||
|
||
/** | ||
* Create a mock diagnostic marker. | ||
* @param range the diagnostic range. | ||
* @param severity the diagnostic severity. | ||
* | ||
* @returns a mock diagnostic marker. | ||
*/ | ||
function createMockMarker(range: Range, severity: DiagnosticSeverity): Readonly<Marker<Diagnostic>> { | ||
const data: Diagnostic = { | ||
range: range, | ||
severity: severity, | ||
message: 'message' | ||
}; | ||
return Object.freeze({ | ||
uri: name, | ||
kind: 'marker', | ||
owner: 'owner', | ||
data | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/******************************************************************************** | ||
* Copyright (C) 2020 Ericsson and others. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License v. 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0. | ||
* | ||
* This Source Code may also be made available under the following Secondary | ||
* Licenses when the conditions for such availability set forth in the Eclipse | ||
* Public License v. 2.0 are satisfied: GNU General Public License, version 2 | ||
* with the GNU Classpath Exception which is available at | ||
* https://www.gnu.org/software/classpath/license.html. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 | ||
********************************************************************************/ | ||
|
||
import { Marker } from '../../common/marker'; | ||
import { Diagnostic } from '@theia/languages/lib/browser'; | ||
|
||
export namespace ProblemUtils { | ||
|
||
/** | ||
* Comparator for severity. | ||
* - The highest severity (`error`) come first followed by others. | ||
* - `undefined` severities are treated as the last ones. | ||
* @param a the first marker for comparison. | ||
* @param b the second marker for comparison. | ||
*/ | ||
export const severityCompare = (a: Marker<Diagnostic>, b: Marker<Diagnostic>): number => | ||
(a.data.severity || Number.MAX_SAFE_INTEGER) - (b.data.severity || Number.MAX_SAFE_INTEGER); | ||
|
||
/** | ||
* Comparator for line numbers. | ||
* - The lowest line number comes first. | ||
* @param a the first marker for comparison. | ||
* @param b the second marker for comparison. | ||
*/ | ||
export const lineNumberCompare = (a: Marker<Diagnostic>, b: Marker<Diagnostic>): number => a.data.range.start.line - b.data.range.start.line; | ||
|
||
/** | ||
* Comparator for column numbers. | ||
* - The lowest column number comes first. | ||
* @param a the first marker for comparison. | ||
* @param b the second marker for comparison. | ||
*/ | ||
export const columnNumberCompare = (a: Marker<Diagnostic>, b: Marker<Diagnostic>): number => a.data.range.start.character - b.data.range.start.character; | ||
|
||
} |