-
-
Notifications
You must be signed in to change notification settings - Fork 6
Update Language Server Protocol #240
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -4,7 +4,26 @@ | |
| /// <reference path="typings-custom/htmlhint.d.ts" /> | ||
|
|
||
| import * as path from "path"; | ||
| import * as server from "vscode-languageserver"; | ||
| import { | ||
| createConnection, | ||
| TextDocuments, | ||
| Diagnostic, | ||
| DiagnosticSeverity, | ||
| ProposedFeatures, | ||
| InitializeParams, | ||
| DidChangeConfigurationNotification, | ||
| CompletionItem, | ||
| CompletionItemKind, | ||
| TextDocumentPositionParams, | ||
| TextDocumentSyncKind, | ||
| InitializeResult, | ||
| Connection, | ||
| ErrorMessageTracker, | ||
| CancellationToken, | ||
| ResponseError, | ||
| InitializeError, | ||
| } from "vscode-languageserver/node"; | ||
| import { TextDocument } from "vscode-languageserver-textdocument"; | ||
| import * as htmlhint from "htmlhint"; | ||
| import fs = require("fs"); | ||
| let stripJsonComments: any = require("strip-json-comments"); | ||
|
|
@@ -80,12 +99,9 @@ | |
| /** | ||
| * Given an htmlhint.Error type return a VS Code server Diagnostic object | ||
| */ | ||
| function makeDiagnostic( | ||
| problem: htmlhint.Error, | ||
| lines: string[], | ||
| ): server.Diagnostic { | ||
| function makeDiagnostic(problem: htmlhint.Error, lines: string[]): Diagnostic { | ||
| return { | ||
| severity: server.DiagnosticSeverity.Warning, | ||
| severity: DiagnosticSeverity.Warning, | ||
| message: problem.message, | ||
| range: getRange(problem, lines), | ||
| code: problem.rule.id, | ||
|
|
@@ -193,21 +209,19 @@ | |
| ); | ||
| } | ||
|
|
||
| function getErrorMessage(err: unknown, document: server.TextDocument): string { | ||
| function getErrorMessage(err: unknown, document: TextDocument): string { | ||
| if (isErrorWithMessage(err)) { | ||
| return err.message; | ||
| } | ||
|
|
||
| return `An unknown error occurred while validating file: ${server.Files.uriToFilePath( | ||
| document.uri, | ||
| )}`; | ||
| return `An unknown error occurred while validating file: ${document.uri}`; | ||
| } | ||
|
|
||
| function validateAllTextDocuments( | ||
| connection: server.IConnection, | ||
| documents: server.TextDocument[], | ||
| connection: Connection, | ||
| documents: TextDocument[], | ||
| ): void { | ||
| let tracker = new server.ErrorMessageTracker(); | ||
| let tracker = new ErrorMessageTracker(); | ||
| documents.forEach((document) => { | ||
| try { | ||
| validateTextDocument(connection, document); | ||
|
|
@@ -219,8 +233,8 @@ | |
| } | ||
|
|
||
| function validateTextDocument( | ||
| connection: server.IConnection, | ||
| document: server.TextDocument, | ||
| connection: Connection, | ||
| document: TextDocument, | ||
| ): void { | ||
| try { | ||
| doValidate(connection, document); | ||
|
|
@@ -229,16 +243,16 @@ | |
| } | ||
| } | ||
|
|
||
| let connection: server.IConnection = server.createConnection(); | ||
| let documents: server.TextDocuments = new server.TextDocuments(); | ||
| let connection: Connection = createConnection(ProposedFeatures.all); | ||
| let documents: TextDocuments<TextDocument> = new TextDocuments(TextDocument); | ||
| documents.listen(connection); | ||
|
|
||
| function trace(message: string, verbose?: string): void { | ||
| connection.tracer.log(message, verbose); | ||
| } | ||
|
|
||
| connection.onInitialize( | ||
| (params: server.InitializeParams, token: server.CancellationToken) => { | ||
| (params: InitializeParams, token: CancellationToken) => { | ||
| let rootFolder = params.rootPath; | ||
| let initOptions: { | ||
| nodePath: string; | ||
|
|
@@ -249,55 +263,34 @@ | |
| : undefined | ||
| : undefined; | ||
|
|
||
| const result = server.Files.resolveModule2( | ||
| rootFolder, | ||
| "htmlhint", | ||
| nodePath, | ||
| trace, | ||
| ).then( | ||
| ( | ||
| value, | ||
| ): | ||
| | server.InitializeResult | ||
| | server.ResponseError<server.InitializeError> => { | ||
| linter = value.default || value.HTMLHint || value; | ||
| //connection.window.showInformationMessage(`onInitialize() - found local htmlhint (version ! ${value.HTMLHint.version})`); | ||
|
|
||
| let result: server.InitializeResult = { | ||
| capabilities: { textDocumentSync: documents.syncKind }, | ||
| }; | ||
| return result; | ||
| }, | ||
| (error) => { | ||
| // didn't find htmlhint in project or global, so use embedded version. | ||
| linter = htmlhint.default || htmlhint.HTMLHint || htmlhint; | ||
| //connection.window.showInformationMessage(`onInitialize() using embedded htmlhint(version ! ${linter.version})`); | ||
| let result: server.InitializeResult = { | ||
| capabilities: { textDocumentSync: documents.syncKind }, | ||
| }; | ||
| return result; | ||
| }, | ||
| ); | ||
| // Since Files API is no longer available, we'll use embedded htmlhint directly | ||
| linter = htmlhint.default || htmlhint.HTMLHint || htmlhint; | ||
|
|
||
| return result as Thenable<server.InitializeResult>; | ||
| let result: InitializeResult = { | ||
| capabilities: { | ||
| textDocumentSync: TextDocumentSyncKind.Incremental, | ||
| }, | ||
| }; | ||
| return result; | ||
| }, | ||
| ); | ||
|
|
||
| function doValidate( | ||
| connection: server.IConnection, | ||
| document: server.TextDocument, | ||
| ): void { | ||
| function doValidate(connection: Connection, document: TextDocument): void { | ||
| try { | ||
| let uri = document.uri; | ||
| let fsPath = server.Files.uriToFilePath(uri); | ||
| // Convert URI to file path manually since Files API is not available | ||
| let fsPath = uri.replace(/^file:\/\//, ""); | ||
| if (process.platform === "win32" && fsPath.startsWith("/")) { | ||
| fsPath = fsPath.substring(1); | ||
| } | ||
|
Comment on lines
+281
to
+285
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider extracting the platform-specific path conversion to a separate function for better readability and maintainability. let uri = document.uri;
// Convert URI to file path manually since Files API is not available
let fsPath = uri.replace(/^file:\/\//, "");
if (process.platform === "win32" && fsPath.startsWith("/")) {
fsPath = fsPath.substring(1);
} |
||
| let contents = document.getText(); | ||
| let lines = contents.split("\n"); | ||
|
|
||
| let config = getConfiguration(fsPath); | ||
|
|
||
| let errors: htmlhint.Error[] = linter.verify(contents, config); | ||
|
|
||
| let diagnostics: server.Diagnostic[] = []; | ||
| let diagnostics: Diagnostic[] = []; | ||
| if (errors.length > 0) { | ||
| errors.forEach((each) => { | ||
| diagnostics.push(makeDiagnostic(each, lines)); | ||
|
|
@@ -334,8 +327,13 @@ | |
| // The watched .htmlhintrc has changed. Clear out the last loaded config, and revalidate all documents. | ||
| connection.onDidChangeWatchedFiles((params) => { | ||
| for (let i = 0; i < params.changes.length; i++) { | ||
| htmlhintrcOptions[server.Files.uriToFilePath(params.changes[i].uri)] = | ||
| undefined; | ||
| // Convert URI to file path manually since Files API is not available | ||
| let uri = params.changes[i].uri; | ||
| let fsPath = uri.replace(/^file:\/\//, ""); | ||
| if (process.platform === "win32" && fsPath.startsWith("/")) { | ||
| fsPath = fsPath.substring(1); | ||
| } | ||
| htmlhintrcOptions[fsPath] = undefined; | ||
| } | ||
| validateAllTextDocuments(connection, documents.all()); | ||
| }); | ||
|
|
||
This file contains hidden or 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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.