Skip to content

Commit

Permalink
Merge pull request redhat-developer#54 from andxu/master
Browse files Browse the repository at this point in the history
fix issue redhat-developer#37 completionHelper will break yaml file if it contains \r …
  • Loading branch information
JPinkney committed Mar 16, 2018
2 parents 6e4eeb8 + 808d7ed commit 94c2d99
Show file tree
Hide file tree
Showing 4 changed files with 192 additions and 191 deletions.
94 changes: 48 additions & 46 deletions src/server.ts
Original file line number Diff line number Diff line change
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 94c2d99

Please sign in to comment.