Skip to content

Commit

Permalink
Added glob setting successfully
Browse files Browse the repository at this point in the history
  • Loading branch information
JPinkney committed Jun 13, 2017
1 parent e2d4126 commit 5f23b18
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 44 deletions.
1 change: 1 addition & 0 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
120 changes: 76 additions & 44 deletions server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string, any, any> = new RequestType('vscode/content');
Expand Down Expand Up @@ -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=>{
Expand All @@ -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 = <Settings>change.settings;
// Revalidate any open text documents
documents.all().forEach(validateTextDocument);
globSetting = settings.k8s.glob || "";
validateValidFiles();
});

let validDocuments: Array<String>;
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(() => {
Expand All @@ -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 =>{
Expand Down Expand Up @@ -159,8 +191,6 @@ function validateTextDocument(textDocument: TextDocument): void {

}

connection.onDidChangeWatchedFiles((change) => {
});

function getLineOffsets(textDocString: String): number[] {

Expand All @@ -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 = <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 = <YAMLDocument> yamlLoader(newText,{});
return languageService.doComplete(document, textDocumentPosition.position, yamlDoc);
}else{

//All the nodes are loaded
let yamlDoc:YAMLDocument = <YAMLDocument> yamlLoader(document.getText(),{});
return languageService.doComplete(document, textDocumentPosition.position, yamlDoc);
}
//All the nodes are loaded
let yamlDoc:YAMLDocument = <YAMLDocument> yamlLoader(document.getText(),{});
return languageService.doComplete(document, textDocumentPosition.position, yamlDoc);
}
}

});

Expand Down

0 comments on commit 5f23b18

Please sign in to comment.