forked from keesschollaart81/vscode-home-assistant
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4f982a4
commit fbbd34a
Showing
7 changed files
with
897 additions
and
181 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
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 |
---|---|---|
@@ -1,81 +1,119 @@ | ||
import { createConnection, TextDocuments, ProposedFeatures, TextDocumentSyncKind, RequestType} from 'vscode-languageserver'; | ||
import { getLanguageService } from './yamlLanguageService'; | ||
import * as path from 'path'; | ||
import { parse as parseYAML } from 'yaml-language-server/out/server/src/languageservice/parser/yamlParser'; | ||
// import * as vscode from 'vscode'; | ||
import { NestedYamlParser, VsCodeFileAccessor } from './yamlDiscovery'; | ||
import * as path from "path"; | ||
import { parse as parseYAML } from "yaml-language-server/out/server/src/languageservice/parser/yamlParser"; | ||
import { YAMLValidation } from "yaml-language-server/out/server/src/languageservice/services/yamlValidation"; | ||
import { JSONSchemaService } from "yaml-language-server/out/server/src/languageservice/services/jsonSchemaService"; | ||
import { | ||
createConnection, | ||
TextDocuments, | ||
ProposedFeatures | ||
} from "vscode-languageserver"; | ||
|
||
import { | ||
NestedYamlParser, | ||
VsCodeFileAccessor, | ||
SchemaServiceForIncludes, | ||
Includetype | ||
} from "./yamlDiscovery"; | ||
|
||
let connection = createConnection(ProposedFeatures.all); | ||
let documents = new TextDocuments(); | ||
let workspaceFolder: string | null; | ||
let parser: NestedYamlParser | null; | ||
let schemaServiceForIncludes: SchemaServiceForIncludes | null; | ||
let yamlvalidation: YAMLValidation | null; | ||
|
||
documents.listen(connection); | ||
|
||
connection.onInitialize((params) => { | ||
workspaceFolder = params.rootUri; | ||
connection.console.log(`[Server(${process.pid}) ${workspaceFolder}] Started and initialize received`); | ||
return { | ||
capabilities: { | ||
textDocumentSync: { | ||
openClose: true, | ||
change: documents.syncKind | ||
} | ||
} | ||
}; | ||
}); | ||
|
||
let workspaceContext = { | ||
|
||
connection.onInitialize(params => { | ||
workspaceFolder = params.rootUri; | ||
|
||
var vsCodeFileAccessor = new VsCodeFileAccessor(workspaceFolder, connection); | ||
parser = new NestedYamlParser(vsCodeFileAccessor); | ||
|
||
let workspaceContext = { | ||
resolveRelativePath: (relativePath: string, resource: string) => { | ||
return path.resolve(resource, relativePath); | ||
return path.resolve(resource, relativePath); | ||
} | ||
}; | ||
}; | ||
|
||
let jsonSchemaService = new JSONSchemaService(null, workspaceContext, null); | ||
schemaServiceForIncludes = new SchemaServiceForIncludes(jsonSchemaService); | ||
|
||
export let languageService = getLanguageService( | ||
workspaceContext, | ||
{ | ||
validation: true | ||
} | ||
); | ||
documents.onDidChangeContent(async (textDocumentChangeEvent) =>{ | ||
if (!textDocumentChangeEvent.document) { | ||
return; | ||
} | ||
yamlvalidation = new YAMLValidation(jsonSchemaService); | ||
yamlvalidation.configure({ | ||
validate: true | ||
}); | ||
|
||
if (textDocumentChangeEvent.document.getText().length === 0) { | ||
return; | ||
connection.console.log( | ||
`[Server(${ | ||
process.pid | ||
}) ${workspaceFolder}] Started and initialize received` | ||
); | ||
return { | ||
capabilities: { | ||
textDocumentSync: { | ||
openClose: true, | ||
change: documents.syncKind | ||
} | ||
} | ||
|
||
|
||
var vsCodeFileAccessor = new VsCodeFileAccessor(workspaceFolder,connection); | ||
var parser = new NestedYamlParser(vsCodeFileAccessor); | ||
var parseResult = await parser.parse([ | ||
path.join(workspaceFolder,"configuration.yaml"), | ||
path.join(workspaceFolder,"ui-lovelace.yaml") | ||
]); | ||
|
||
let yamlDocument = parseYAML(textDocumentChangeEvent.document.getText(), []); | ||
if (!yamlDocument) { | ||
return; | ||
} | ||
languageService.doValidation(textDocumentChangeEvent.document, yamlDocument).then((diagnosticResults) => { | ||
|
||
if (!diagnosticResults) { | ||
return; | ||
} | ||
let diagnostics = []; | ||
for (let diagnosticItem in diagnosticResults) { | ||
diagnosticResults[diagnosticItem].severity = 1; //Convert all warnings to errors | ||
diagnostics.push(diagnosticResults[diagnosticItem]); | ||
} | ||
connection.sendDiagnostics({ uri: textDocumentChangeEvent.document.uri, diagnostics: diagnostics }); | ||
}, (error) => { | ||
connection.window.showErrorMessage(`oops: ${error}`); | ||
}); | ||
}) | ||
|
||
documents.onDidOpen((event) => { | ||
connection.console.log(`[Server(${process.pid}) ${workspaceFolder}] Document opened: ${event.document.uri}`); | ||
}); | ||
}; | ||
}); | ||
|
||
documents.onDidChangeContent(async textDocumentChangeEvent => { | ||
if (!textDocumentChangeEvent.document) { | ||
return; | ||
} | ||
|
||
var parseResult = await parser.parse([ | ||
path.join(workspaceFolder, "configuration.yaml"), | ||
path.join(workspaceFolder, "ui-lovelace.yaml") | ||
]); | ||
|
||
schemaServiceForIncludes.onUpdate(parseResult.filePathMappings); | ||
|
||
if (textDocumentChangeEvent.document.getText().length === 0) { | ||
return; | ||
} | ||
|
||
let yamlDocument = parseYAML(textDocumentChangeEvent.document.getText(), getValidYamlTags()); | ||
if (!yamlDocument) { | ||
return; | ||
} | ||
var diagnosticResults = await yamlvalidation.doValidation( | ||
textDocumentChangeEvent.document, | ||
yamlDocument | ||
); | ||
|
||
if (!diagnosticResults) { | ||
return; | ||
} | ||
let diagnostics = []; | ||
for (let diagnosticItem in diagnosticResults) { | ||
diagnosticResults[diagnosticItem].severity = 1; //Convert all warnings to errors | ||
diagnostics.push(diagnosticResults[diagnosticItem]); | ||
} | ||
connection.sendDiagnostics({ | ||
uri: textDocumentChangeEvent.document.uri, | ||
diagnostics: diagnostics | ||
}); | ||
}); | ||
|
||
documents.onDidOpen(event => { | ||
connection.console.log( | ||
`[Server(${process.pid}) ${workspaceFolder}] Document opened: ${ | ||
event.document.uri | ||
}` | ||
); | ||
}); | ||
|
||
function getValidYamlTags() : string[] { | ||
var validTags: string[] = []; | ||
for (let item in Includetype) { | ||
if (isNaN(Number(item))) { | ||
validTags.push(`!${item} scalar`); | ||
} | ||
} | ||
validTags.push("!secret scalar"); | ||
return validTags; | ||
} | ||
connection.listen(); | ||
|
Oops, something went wrong.