-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
markers: apply problem marker sorting based on severity, line, and column number #7313
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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; | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(nit) I would better to add them as static method to
ProblemTree
.Utils
always feels like it is useful to share but I don't know what is a good concept for it.