Skip to content

Commit

Permalink
Add a more realistic diagnostic provider
Browse files Browse the repository at this point in the history
  • Loading branch information
ddaspit committed Nov 1, 2024
1 parent 2f63b93 commit c981c51
Show file tree
Hide file tree
Showing 11 changed files with 164 additions and 184 deletions.
1 change: 1 addition & 0 deletions packages/core/src/diagnostic/diagnostic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ export interface Diagnostic {
range: Range;
severity: DiagnosticSeverity;
message: string;
data?: unknown;
}
7 changes: 6 additions & 1 deletion packages/core/src/diagnostic/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
export type { Diagnostic } from './diagnostic';
export { DiagnosticSeverity } from './diagnostic';
export type { DiagnosticFix } from './diagnostic-fix';
export type { DiagnosticProvider, DiagnosticProviderFactory, DiagnosticsChanged } from './diagnostic-provider';
export type {
DiagnosticProvider,
DiagnosticProviderConstructor,
DiagnosticProviderFactory,
DiagnosticsChanged,
} from './diagnostic-provider';
13 changes: 5 additions & 8 deletions packages/core/src/document/scripture-container.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Position } from '../common/position';
import { Range } from '../common/range';
import { ScriptureDocument } from './scripture-document';
import { ScriptureNode, ScriptureNodeType } from './scripture-node';
import { findNodes, ScriptureNode, ScriptureNodeType } from './scripture-node';

export abstract class ScriptureContainer implements ScriptureNode {
private _parent?: ScriptureNode;
Expand Down Expand Up @@ -49,13 +49,10 @@ export abstract class ScriptureContainer implements ScriptureNode {
return this.document.getText(this.range);
}

*getNodes(filter?: ScriptureNodeType | ((node: ScriptureNode) => boolean)): IterableIterator<ScriptureNode> {
for (const child of this._children) {
if (filter == null || child.type === filter || (typeof filter === 'function' && filter(child))) {
yield child;
}
yield* child.getNodes(filter);
}
findNodes(
filter?: ScriptureNodeType | ((node: ScriptureNode) => boolean) | ScriptureNodeType[],
): IterableIterator<ScriptureNode> {
return findNodes(this, filter);
}

positionAt(offset: number): Position {
Expand Down
13 changes: 5 additions & 8 deletions packages/core/src/document/scripture-document.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Range } from '../common';
import { Document } from './document';
import { ScriptureNode, ScriptureNodeType } from './scripture-node';
import { findNodes, ScriptureNode, ScriptureNodeType } from './scripture-node';
import { TextDocument } from './text-document';

export class ScriptureDocument extends TextDocument implements Document, ScriptureNode {
Expand Down Expand Up @@ -37,13 +37,10 @@ export class ScriptureDocument extends TextDocument implements Document, Scriptu
throw new Error('The method is not supported.');
}

*getNodes(filter?: ScriptureNodeType | ((node: ScriptureNode) => boolean)): IterableIterator<ScriptureNode> {
for (const child of this._children) {
if (filter == null || child.type === filter || (typeof filter === 'function' && filter(child))) {
yield child;
}
yield* child.getNodes(filter);
}
findNodes(
filter?: ScriptureNodeType | ((node: ScriptureNode) => boolean) | ScriptureNodeType[],
): IterableIterator<ScriptureNode> {
return findNodes(this, filter);
}

appendChild(child: ScriptureNode): void {
Expand Down
4 changes: 3 additions & 1 deletion packages/core/src/document/scripture-leaf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ export abstract class ScriptureLeaf implements ScriptureNode {
return this.document.getText(this.range);
}

*getNodes(_filter?: ScriptureNodeType | ((node: ScriptureNode) => boolean)): IterableIterator<ScriptureNode> {
*findNodes(
_filter?: ScriptureNodeType | ((node: ScriptureNode) => boolean) | ScriptureNodeType[],
): IterableIterator<ScriptureNode> {
// return nothing
}

Expand Down
19 changes: 18 additions & 1 deletion packages/core/src/document/scripture-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,28 @@ export interface ScriptureNode {
updateParent(parent: ScriptureNode | undefined): void;
remove(): void;
getText(): string;
getNodes(filter?: ScriptureNodeType | ((node: ScriptureNode) => boolean)): IterableIterator<ScriptureNode>;
findNodes(filter?: ScriptureNodeType | ((node: ScriptureNode) => boolean)): IterableIterator<ScriptureNode>;
positionAt(offset: number): Position;
appendChild(child: ScriptureNode): void;
insertChild(index: number, child: ScriptureNode): void;
removeChild(child: ScriptureNode): void;
spliceChildren(start: number, deleteCount: number, ...items: ScriptureNode[]): void;
clearChildren(): void;
}

export function* findNodes(
node: ScriptureNode,
filter?: ScriptureNodeType | ((node: ScriptureNode) => boolean) | ScriptureNodeType[],
): IterableIterator<ScriptureNode> {
for (const child of node.children) {
if (
filter == null ||
(Array.isArray(filter) && filter.includes(child.type)) ||
(typeof filter === 'function' && filter(child)) ||
child.type === filter
) {
yield child;
}
yield* findNodes(child, filter);
}
}
6 changes: 5 additions & 1 deletion packages/core/src/formatting/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
export type { OnTypeFormattingProvider, OnTypeFormattingProviderFactory } from './on-type-formatting-provider';
export type {
OnTypeFormattingProvider,
OnTypeFormattingProviderConstructor,
OnTypeFormattingProviderFactory,
} from './on-type-formatting-provider';
23 changes: 1 addition & 22 deletions packages/vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,28 +31,7 @@
"USFM"
]
}
],
"configuration": {
"type": "object",
"title": "Lynx Test",
"properties": {
"lynxTest.maxNumberOfProblems": {
"type": "number",
"default": 100,
"description": "Controls the maximum number of problems produced by the server."
},
"lynxTest.trace.server": {
"type": "string",
"enum": [
"off",
"messages",
"verbose"
],
"default": "off",
"description": "Traces the communication between VS Code and the language server."
}
}
}
]
},
"dependencies": {
"lynx-core": "*",
Expand Down
24 changes: 2 additions & 22 deletions packages/vscode/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,7 @@ import {
} from 'vscode-languageserver/node';

import { SmartQuoteFormattingProvider } from './smart-quote-formatting-provider';
import { TestDiagnosticProvider, TestDiagnosticProviderConfig } from './test-diagnostic-provider';

// The global settings, used when the `workspace/configuration` request is not supported by the client.
// Please note that this is not the case when using this server with the client provided in this example
// but could happen with other clients.
const defaultSettings: TestDiagnosticProviderConfig = { maxNumberOfProblems: 1000 };
let globalSettings: TestDiagnosticProviderConfig = defaultSettings;
import { VerseOrderDiagnosticProvider } from './verse-order-diagnostic-provider';

// Create a connection for the server, using Node's IPC as a transport.
// Also include all preview / proposed LSP features.
Expand All @@ -29,7 +23,7 @@ const connection = createConnection(ProposedFeatures.all);
// Create a simple text document manager.
const workspace = new Workspace({
documentFactory: new UsfmDocumentFactory(new UsfmStylesheet('usfm.sty')),
diagnosticProviders: [TestDiagnosticProvider.factory(() => globalSettings)],
diagnosticProviders: [VerseOrderDiagnosticProvider],
onTypeFormattingProviders: [SmartQuoteFormattingProvider],
});

Expand Down Expand Up @@ -83,15 +77,6 @@ connection.onInitialized(() => {
}
});

connection.onDidChangeConfiguration((change) => {
const settings = change.settings as Map<string, unknown>;
globalSettings = (settings.get('lynxTest') as TestDiagnosticProviderConfig | undefined) ?? defaultSettings;
// Refresh the diagnostics since the `maxNumberOfProblems` could have changed.
// We could optimize things here and re-fetch the setting first can compare it
// to the existing setting, but this is out of scope for this example.
connection.languages.diagnostics.refresh();
});

connection.languages.diagnostics.on(async (params) => {
return {
kind: DocumentDiagnosticReportKind.Full,
Expand Down Expand Up @@ -143,10 +128,5 @@ connection.onDocumentOnTypeFormatting(async (params) => {
return await workspace.getOnTypeEdits(params.textDocument.uri, params.position, params.ch);
});

connection.onDidChangeWatchedFiles((_change) => {
// Monitored files have change in VSCode
connection.console.log('We received a file change event');
});

// Listen on the connection
connection.listen();
120 changes: 0 additions & 120 deletions packages/vscode/src/test-diagnostic-provider.ts

This file was deleted.

Loading

0 comments on commit c981c51

Please sign in to comment.