diff --git a/server/package.json b/server/package.json index ae13f328..9dd68b02 100755 --- a/server/package.json +++ b/server/package.json @@ -9,6 +9,7 @@ }, "dependencies": { "@types/mocha": "^2.2.41", + "glob": "^7.1.2", "jsonc-parser": "^0.4.2", "mocha": "^3.4.2", "request-light": "^0.2.0", diff --git a/server/src/server.ts b/server/src/server.ts index 267bdee7..5f5d05e0 100755 --- a/server/src/server.ts +++ b/server/src/server.ts @@ -14,6 +14,7 @@ import Strings = require( './languageService/utils/strings'); import URI from './languageService/utils/uri'; import * as URL from 'url'; import fs = require('fs'); +var glob = require('glob'); namespace VSCodeContentRequest { export const type: RequestType = new RequestType('vscode/content'); @@ -88,8 +89,9 @@ let languageService = getLanguageService(schemaRequestService, workspaceContext) // The content of a text document has changed. This event is emitted // when the text document first opened or when its content has changed. documents.onDidChangeContent((change) => { - console.log("hit"); - triggerValidation(change.document); + if(validDocuments.indexOf(change.document.uri) !== -1){ + triggerValidation(change.document); + } }); documents.onDidClose((event=>{ @@ -99,17 +101,47 @@ documents.onDidClose((event=>{ // The settings interface describe the server relevant settings part interface Settings { + k8s: globSetting; +} + +interface globSetting { + glob: string; } -// hold the maxNumberOfProblems setting -// The settings have changed. Is send on server activation -// as well. +let globSetting: string; connection.onDidChangeConfiguration((change) => { let settings = change.settings; - // Revalidate any open text documents - documents.all().forEach(validateTextDocument); + globSetting = settings.k8s.glob || ""; + validateValidFiles(); }); +let validDocuments: Array; +function validateValidFiles(){ + //Clear all the previous diagnostics + documents.all().forEach(doc => { + connection.sendDiagnostics({ uri: doc.uri, diagnostics: [] }); + }); + + validDocuments = []; + glob(globSetting, function (er, files) { + if(er){ + throw er; + } + + files.forEach(file => { + documents.all().forEach(doc => { + let splitDocumentUri = doc.uri.split("/"); + let strippedDocumentUri = splitDocumentUri[splitDocumentUri.length - 1]; + if(strippedDocumentUri.indexOf(file) !== -1){ + validDocuments.push(doc.uri); + triggerValidation(doc); + } + } + )}); + + }) +} + function triggerValidation(textDocument: TextDocument): void { cleanPendingValidation(textDocument); pendingValidationRequests[textDocument.uri] = setTimeout(() => { @@ -128,7 +160,7 @@ function cleanPendingValidation(textDocument: TextDocument): void { function validateTextDocument(textDocument: TextDocument): void { let yDoc= yamlLoader(textDocument.getText(),{}); - if(yDoc !== undefined){ + if(yDoc !== undefined){ let diagnostics = []; if(yDoc.errors.length != 0){ diagnostics = yDoc.errors.map(error =>{ @@ -159,8 +191,6 @@ function validateTextDocument(textDocument: TextDocument): void { } -connection.onDidChangeWatchedFiles((change) => { -}); function getLineOffsets(textDocString: String): number[] { @@ -187,42 +217,44 @@ function getLineOffsets(textDocString: String): number[] { // This handler provides the initial list of the completion items. connection.onCompletion(textDocumentPosition => { - let document = documents.get(textDocumentPosition.textDocument.uri); + let document = documents.get(textDocumentPosition.textDocument.uri); + if(validDocuments.indexOf(document.uri) !== -1){ + + /* + * THIS IS A HACKY VERSION. + * Needed to get the parent node from the current node to support autocompletion. + */ + + //Get the string we are looking at via a substring + let start = getLineOffsets(document.getText())[textDocumentPosition.position.line]; + let end = document.offsetAt(textDocumentPosition.position); + let textLine = document.getText().substring(start, end); + + //Check if the string we are looking at is a node + if(textLine.indexOf(":")){ + //We need to add the ":" to load the nodes + + let newText = ""; + + //This is for the empty line case + if(textLine.trim().length === 0){ + //Add a temp node that is in the document but we don't use at all. + newText = document.getText().substring(0, end) + "holder:\r\n" + document.getText().substr(end+2) + //For when missing semi colon case + }else{ + //Add a semicolon to the end of the current line so we can validate the node + newText = document.getText().substring(0, end) + ":\r\n" + document.getText().substr(end+2) + } - /* - * THIS IS A HACKY VERSION. - * Needed to get the parent node from the current node to support autocompletion. - */ + let yamlDoc:YAMLDocument = yamlLoader(newText,{}); + return languageService.doComplete(document, textDocumentPosition.position, yamlDoc); + }else{ - //Get the string we are looking at via a substring - let start = getLineOffsets(document.getText())[textDocumentPosition.position.line]; - let end = document.offsetAt(textDocumentPosition.position); - let textLine = document.getText().substring(start, end); - - //Check if the string we are looking at is a node - if(textLine.indexOf(":")){ - //We need to add the ":" to load the nodes - - let newText = ""; - - //This is for the empty line case - if(textLine.trim().length === 0){ - //Add a temp node that is in the document but we don't use at all. - newText = document.getText().substring(0, end) + "holder:\r\n" + document.getText().substr(end+2) - //For when missing semi colon case - }else{ - //Add a semicolon to the end of the current line so we can validate the node - newText = document.getText().substring(0, end) + ":\r\n" + document.getText().substr(end+2) - } - - let yamlDoc:YAMLDocument = yamlLoader(newText,{}); - return languageService.doComplete(document, textDocumentPosition.position, yamlDoc); - }else{ - - //All the nodes are loaded - let yamlDoc:YAMLDocument = yamlLoader(document.getText(),{}); - return languageService.doComplete(document, textDocumentPosition.position, yamlDoc); - } + //All the nodes are loaded + let yamlDoc:YAMLDocument = yamlLoader(document.getText(),{}); + return languageService.doComplete(document, textDocumentPosition.position, yamlDoc); + } + } });