chore: implemented a O(n) unique and added unit tests (#4429)
This commit is contained in:
81
src/editor/manager.test.ts
Normal file
81
src/editor/manager.test.ts
Normal file
@ -0,0 +1,81 @@
|
|||||||
|
import { editorManager } from 'lib/singletons'
|
||||||
|
import { Diagnostic } from '@codemirror/lint'
|
||||||
|
|
||||||
|
describe('EditorManager Class', () => {
|
||||||
|
describe('makeUniqueDiagnostics', () => {
|
||||||
|
it('should filter out duplicated diagnostics', () => {
|
||||||
|
const duplicatedDiagnostics: Diagnostic[] = [
|
||||||
|
{
|
||||||
|
from: 2,
|
||||||
|
to: 10,
|
||||||
|
severity: 'hint',
|
||||||
|
message: 'my cool message',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
from: 2,
|
||||||
|
to: 10,
|
||||||
|
severity: 'hint',
|
||||||
|
message: 'my cool message',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
from: 2,
|
||||||
|
to: 10,
|
||||||
|
severity: 'hint',
|
||||||
|
message: 'my cool message',
|
||||||
|
},
|
||||||
|
]
|
||||||
|
|
||||||
|
const expected: Diagnostic[] = [
|
||||||
|
{
|
||||||
|
from: 2,
|
||||||
|
to: 10,
|
||||||
|
severity: 'hint',
|
||||||
|
message: 'my cool message',
|
||||||
|
},
|
||||||
|
]
|
||||||
|
|
||||||
|
const actual = editorManager.makeUniqueDiagnostics(duplicatedDiagnostics)
|
||||||
|
expect(actual).toStrictEqual(expected)
|
||||||
|
})
|
||||||
|
it('should filter out duplicated diagnostic and keep some original ones', () => {
|
||||||
|
const duplicatedDiagnostics: Diagnostic[] = [
|
||||||
|
{
|
||||||
|
from: 0,
|
||||||
|
to: 10,
|
||||||
|
severity: 'hint',
|
||||||
|
message: 'my cool message',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
from: 0,
|
||||||
|
to: 10,
|
||||||
|
severity: 'hint',
|
||||||
|
message: 'my cool message',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
from: 88,
|
||||||
|
to: 99,
|
||||||
|
severity: 'hint',
|
||||||
|
message: 'my super cool message',
|
||||||
|
},
|
||||||
|
]
|
||||||
|
|
||||||
|
const expected: Diagnostic[] = [
|
||||||
|
{
|
||||||
|
from: 0,
|
||||||
|
to: 10,
|
||||||
|
severity: 'hint',
|
||||||
|
message: 'my cool message',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
from: 88,
|
||||||
|
to: 99,
|
||||||
|
severity: 'hint',
|
||||||
|
message: 'my super cool message',
|
||||||
|
},
|
||||||
|
]
|
||||||
|
|
||||||
|
const actual = editorManager.makeUniqueDiagnostics(duplicatedDiagnostics)
|
||||||
|
expect(actual).toStrictEqual(expected)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
@ -24,10 +24,6 @@ export const modelingMachineEvent = modelingMachineAnnotation.of(true)
|
|||||||
const setDiagnosticsAnnotation = Annotation.define<boolean>()
|
const setDiagnosticsAnnotation = Annotation.define<boolean>()
|
||||||
export const setDiagnosticsEvent = setDiagnosticsAnnotation.of(true)
|
export const setDiagnosticsEvent = setDiagnosticsAnnotation.of(true)
|
||||||
|
|
||||||
function diagnosticIsEqual(d1: Diagnostic, d2: Diagnostic): boolean {
|
|
||||||
return d1.from === d2.from && d1.to === d2.to && d1.message === d2.message
|
|
||||||
}
|
|
||||||
|
|
||||||
export default class EditorManager {
|
export default class EditorManager {
|
||||||
private _editorView: EditorView | null = null
|
private _editorView: EditorView | null = null
|
||||||
private _copilotEnabled: boolean = true
|
private _copilotEnabled: boolean = true
|
||||||
@ -162,20 +158,29 @@ export default class EditorManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Given an array of Diagnostics remove any duplicates by hashing a key
|
||||||
|
* in the format of from + ' ' + to + ' ' + message.
|
||||||
|
*/
|
||||||
|
makeUniqueDiagnostics(duplicatedDiagnostics: Diagnostic[]): Diagnostic[] {
|
||||||
|
const uniqueDiagnostics: Diagnostic[] = []
|
||||||
|
const seenDiagnostic: { [key: string]: boolean } = {}
|
||||||
|
|
||||||
|
duplicatedDiagnostics.forEach((diagnostic: Diagnostic) => {
|
||||||
|
const hash = `${diagnostic.from} ${diagnostic.to} ${diagnostic.message}`
|
||||||
|
if (!seenDiagnostic[hash]) {
|
||||||
|
uniqueDiagnostics.push(diagnostic)
|
||||||
|
seenDiagnostic[hash] = true
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
return uniqueDiagnostics
|
||||||
|
}
|
||||||
|
|
||||||
setDiagnostics(diagnostics: Diagnostic[]): void {
|
setDiagnostics(diagnostics: Diagnostic[]): void {
|
||||||
if (!this._editorView) return
|
if (!this._editorView) return
|
||||||
// Clear out any existing diagnostics that are the same.
|
// Clear out any existing diagnostics that are the same.
|
||||||
for (const diagnostic of diagnostics) {
|
diagnostics = this.makeUniqueDiagnostics(diagnostics)
|
||||||
for (const otherDiagnostic of diagnostics) {
|
|
||||||
if (diagnosticIsEqual(diagnostic, otherDiagnostic)) {
|
|
||||||
diagnostics = diagnostics.filter(
|
|
||||||
(d) => !diagnosticIsEqual(d, diagnostic)
|
|
||||||
)
|
|
||||||
diagnostics.push(diagnostic)
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
this._editorView.dispatch({
|
this._editorView.dispatch({
|
||||||
effects: [setDiagnosticsEffect.of(diagnostics)],
|
effects: [setDiagnosticsEffect.of(diagnostics)],
|
||||||
|
@ -125,7 +125,7 @@ export class KclManager {
|
|||||||
if (this.lints.length > 0) {
|
if (this.lints.length > 0) {
|
||||||
diagnostics = diagnostics.concat(this.lints)
|
diagnostics = diagnostics.concat(this.lints)
|
||||||
}
|
}
|
||||||
editorManager.setDiagnostics(diagnostics)
|
editorManager?.setDiagnostics(diagnostics)
|
||||||
}
|
}
|
||||||
|
|
||||||
addKclErrors(kclErrors: KCLError[]) {
|
addKclErrors(kclErrors: KCLError[]) {
|
||||||
|
Reference in New Issue
Block a user