Skip to content

Commit

Permalink
fix isee #37 completionHelper will break yaml file if it contains \r …
Browse files Browse the repository at this point in the history
…charactor at #37
  • Loading branch information
andxu committed Mar 16, 2018
1 parent 9cbe196 commit 808d7ed
Show file tree
Hide file tree
Showing 4 changed files with 193 additions and 192 deletions.
96 changes: 49 additions & 47 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ function validateTextDocument(textDocument: TextDocument): void {
connection.sendDiagnostics({ uri: textDocument.uri, diagnostics: [] });
return;
}

let yamlDocument = parseYAML(textDocument.getText());
isKubernetes(textDocument) ? setKubernetesParserOption(yamlDocument.documents, true) : setKubernetesParserOption(yamlDocument.documents, false);
customLanguageService.doValidation(textDocument, yamlDocument).then(function(diagnosticResults){
Expand Down Expand Up @@ -450,60 +450,62 @@ connection.onCompletion(textDocumentPosition => {
return customLanguageService.doComplete(textDocument, textDocumentPosition.position, jsonDocument);
});

function is_EOL(c) {
return (c === 0x0A/* LF */) || (c === 0x0D/* CR */);
}

function completionHelper(document: TextDocument, textDocumentPosition: Position){

//Get the string we are looking at via a substring
let linePos = textDocumentPosition.line;
let position = textDocumentPosition;
let lineOffset = getLineOffsets(document.getText());
let start = lineOffset[linePos]; //Start of where the autocompletion is happening
let end = 0; //End of where the autocompletion is happening
if(lineOffset[linePos+1]){
end = lineOffset[linePos+1];
//Get the string we are looking at via a substring
let linePos = textDocumentPosition.line;
let position = textDocumentPosition;
let lineOffset = getLineOffsets(document.getText());
let start = lineOffset[linePos]; //Start of where the autocompletion is happening
let end = 0; //End of where the autocompletion is happening
if(lineOffset[linePos+1]){
end = lineOffset[linePos+1];
}else{
end = document.getText().length;
}

while (end - 1 >= 0 && is_EOL(document.getText().charCodeAt(end - 1))) {
end--;
}

let textLine = document.getText().substring(start, end);

//Check if the string we are looking at is a node
if(textLine.indexOf(":") === -1){
//We need to add the ":" to load the nodes

let newText = "";

//This is for the empty line case
let trimmedText = textLine.trim();
if(trimmedText.length === 0 || (trimmedText.length === 1 && trimmedText[0] === '-')){
//Add a temp node that is in the document but we don't use at all.
newText = document.getText().substring(0, start+textLine.length) + "holder:\r\n" + document.getText().substr(lineOffset[linePos+1] || document.getText().length);

//For when missing semi colon case
}else{
end = document.getText().length;
//Add a semicolon to the end of the current line so we can validate the node
newText = document.getText().substring(0, start+textLine.length) + ":\r\n" + document.getText().substr(lineOffset[linePos+1] || document.getText().length);
}
let textLine = document.getText().substring(start, end);

//Check if the string we are looking at is a node
if(textLine.indexOf(":") === -1){
//We need to add the ":" to load the nodes

let newText = "";

//This is for the empty line case
let trimmedText = textLine.trim();
if(trimmedText.length === 0 || (trimmedText.length === 1 && trimmedText[0] === '-')){
//Add a temp node that is in the document but we don't use at all.
if(lineOffset[linePos+1]){
newText = document.getText().substring(0, start+(textLine.length-1)) + "holder:\r\n" + document.getText().substr(end+2);
}else{
newText = document.getText().substring(0, start+(textLine.length)) + "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
if(lineOffset[linePos+1]){
newText = document.getText().substring(0, start+(textLine.length-1)) + ":\r\n" + document.getText().substr(end+2);
}else{
newText = document.getText().substring(0, start+(textLine.length)) + ":\r\n" + document.getText().substr(end+2);
}
}

return {
"newText": newText,
"newPosition": textDocumentPosition
}
return {
"newText": newText,
"newPosition": textDocumentPosition
}

}else{
}else{

//All the nodes are loaded
position.character = position.character - 1;
return {
"newText": document.getText(),
"newPosition": position
}
//All the nodes are loaded
position.character = position.character - 1;
return {
"newText": document.getText(),
"newPosition": position
}
}

}

Expand Down
109 changes: 54 additions & 55 deletions test/autoCompletion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ languageService.configure(languageSettings);

suite("Auto Completion Tests", () => {


describe('yamlCompletion with bowerrc', function(){

describe('doComplete', function(){

function setup(content: string){
return TextDocument.create("file://~/Desktop/vscode-k8s/test.yaml", "yaml", 0, content);
}
Expand All @@ -55,7 +55,7 @@ suite("Auto Completion Tests", () => {
let content = "";
let completion = parseSetup(content, 0);
completion.then(function(result){
assert.notEqual(result.items.length, 0);
assert.notEqual(result.items.length, 0);
}).then(done, done);
});

Expand Down Expand Up @@ -123,39 +123,39 @@ suite("Auto Completion Tests", () => {
}).then(done, done);
});

it('Autocomplete key in middle of file 2', (done) => {
it('Autocomplete key in middle of file 2', (done) => {
let content = "scripts:\n postinstall: /test\n preinsta";
let completion = parseSetup(content, 31);
completion.then(function(result){
assert.notEqual(result.items.length, 0);
}).then(done, done);
});

it('Autocomplete does not happen right after :', (done) => {
it('Autocomplete does not happen right after :', (done) => {
let content = "analytics:";
let completion = parseSetup(content, 9);
completion.then(function(result){
assert.notEqual(result.items.length, 0);
}).then(done, done);
});

it('Autocomplete does not happen right after : under an object', (done) => {
it('Autocomplete does not happen right after : under an object', (done) => {
let content = "scripts:\n postinstall:";
let completion = parseSetup(content, 21);
completion.then(function(result){
assert.notEqual(result.items.length, 0);
}).then(done, done);
});

it('Autocomplete on multi yaml documents in a single file on root', (done) => {
it('Autocomplete on multi yaml documents in a single file on root', (done) => {
let content = `---\nanalytics: true\n...\n---\n...`;
let completion = parseSetup(content, 28);
completion.then(function(result){
assert.notEqual(result.items.length, 0);
}).then(done, done);
});

it('Autocomplete on multi yaml documents in a single file on scalar', (done) => {
it('Autocomplete on multi yaml documents in a single file on scalar', (done) => {
let content = `---\nanalytics: true\n...\n---\njson: \n...`;
let completion = parseSetup(content, 34);
completion.then(function(result){
Expand All @@ -166,55 +166,54 @@ suite("Auto Completion Tests", () => {
});
});

function is_EOL(c) {
return (c === 0x0A/* LF */) || (c === 0x0D/* CR */);
}

function completionHelper(document: TextDocument, textDocumentPosition){

//Get the string we are looking at via a substring
let linePos = textDocumentPosition.line;
let position = textDocumentPosition;
let lineOffset = getLineOffsets(document.getText());
let start = lineOffset[linePos]; //Start of where the autocompletion is happening
let end = 0; //End of where the autocompletion is happening
if(lineOffset[linePos+1]){
end = lineOffset[linePos+1];
}else{
end = document.getText().length;
}
let textLine = document.getText().substring(start, end);

//Check if the string we are looking at is a node
if(textLine.indexOf(":") === -1){
//We need to add the ":" to load the nodes

let newText = "";

//This is for the empty line case
let trimmedText = textLine.trim();
if(trimmedText.length === 0 || (trimmedText.length === 1 && trimmedText[0] === '-')){

//Add a temp node that is in the document but we don't use at all.
if(lineOffset[linePos+1]){
newText = document.getText().substring(0, start+(textLine.length-1)) + "holder:\r\n" + document.getText().substr(end+2);
}else{
newText = document.getText().substring(0, start+(textLine.length)) + "holder:\r\n" + document.getText().substr(end+2);
}


//Get the string we are looking at via a substring
let linePos = textDocumentPosition.line;
let position = textDocumentPosition;
let lineOffset = getLineOffsets(document.getText());
let start = lineOffset[linePos]; //Start of where the autocompletion is happening
let end = 0; //End of where the autocompletion is happening
if(lineOffset[linePos+1]){
end = lineOffset[linePos+1];
}else{
end = document.getText().length;
}

while (end - 1 >= 0 && is_EOL(document.getText().charCodeAt(end - 1))) {
end--;
}

let textLine = document.getText().substring(start, end);

//Check if the string we are looking at is a node
if(textLine.indexOf(":") === -1){
//We need to add the ":" to load the nodes

let newText = "";

//This is for the empty line case
let trimmedText = textLine.trim();
if(trimmedText.length === 0 || (trimmedText.length === 1 && trimmedText[0] === '-')){
//Add a temp node that is in the document but we don't use at all.
newText = document.getText().substring(0, start+textLine.length) + "holder:\r\n" + document.getText().substr(lineOffset[linePos+1] || document.getText().length);
//For when missing semi colon case
}else{
//Add a semicolon to the end of the current line so we can validate the node
if(lineOffset[linePos+1]){
newText = document.getText().substring(0, start+(textLine.length-1)) + ":\r\n" + document.getText().substr(end+2);
}else{
newText = document.getText().substring(0, start+(textLine.length)) + ":\r\n" + document.getText().substr(end+2);
}
}
let jsonDocument = parseYAML(newText);
return languageService.doComplete(document, position, jsonDocument);
}else{

//All the nodes are loaded
position.character = position.character - 1;
let jsonDocument = parseYAML(document.getText());
return languageService.doComplete(document, position, jsonDocument);
//Add a semicolon to the end of the current line so we can validate the node
newText = document.getText().substring(0, start+textLine.length) + ":\r\n" + document.getText().substr(lineOffset[linePos+1] || document.getText().length);
}
let jsonDocument = parseYAML(newText);
return languageService.doComplete(document, position, jsonDocument);
}else{

//All the nodes are loaded
position.character = position.character - 1;
let jsonDocument = parseYAML(document.getText());
return languageService.doComplete(document, position, jsonDocument);
}

}
Loading

0 comments on commit 808d7ed

Please sign in to comment.