Skip to content

Commit fcceb1d

Browse files
changed parser when last line is empty or null (#625)
* changed parser when lastline is empty or null * getting text tokens using Lexer * slice last two tokens from array * check whether last line empty or not using textdocument'
1 parent 8b858ad commit fcceb1d

File tree

4 files changed

+38
-5
lines changed

4 files changed

+38
-5
lines changed

src/languageservice/parser/yaml-documents.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ export class YamlDocuments {
250250
if (addRootObject && !/\S/.test(text)) {
251251
text = `{${text}}`;
252252
}
253-
const doc = parseYAML(text, parserOptions);
253+
const doc = parseYAML(text, parserOptions, document);
254254
cacheEntry.document = doc;
255255
cacheEntry.version = document.version;
256256
cacheEntry.parserOptions = parserOptions;

src/languageservice/parser/yamlParser07.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import { Parser, Composer, Document, LineCounter, ParseOptions, DocumentOptions, SchemaOptions } from 'yaml';
99
import { YAMLDocument, SingleYAMLDocument } from './yaml-documents';
1010
import { getCustomTags } from './custom-tag-provider';
11+
import { TextDocument } from 'vscode-languageserver-textdocument';
12+
import { TextBuffer } from '../utils/textBuffer';
1113

1214
export { YAMLDocument, SingleYAMLDocument };
1315

@@ -25,7 +27,7 @@ export const defaultOptions: ParserOptions = {
2527
* returns YAML AST nodes, which are then formatted
2628
* for consumption via the language server.
2729
*/
28-
export function parse(text: string, parserOptions: ParserOptions = defaultOptions): YAMLDocument {
30+
export function parse(text: string, parserOptions: ParserOptions = defaultOptions, document?: TextDocument): YAMLDocument {
2931
const options: ParseOptions & DocumentOptions & SchemaOptions = {
3032
strict: false,
3133
customTags: getCustomTags(parserOptions.customTags),
@@ -34,7 +36,14 @@ export function parse(text: string, parserOptions: ParserOptions = defaultOption
3436
};
3537
const composer = new Composer(options);
3638
const lineCounter = new LineCounter();
37-
const parser = new Parser(lineCounter.addNewLine);
39+
let isLastLineEmpty = false;
40+
if (document) {
41+
const textBuffer = new TextBuffer(document);
42+
const position = textBuffer.getPosition(text.length);
43+
const lineContent = textBuffer.getLineContent(position.line);
44+
isLastLineEmpty = lineContent.trim().length === 0;
45+
}
46+
const parser = isLastLineEmpty ? new Parser() : new Parser(lineCounter.addNewLine);
3847
const tokens = parser.parse(text);
3948
const tokensArr = Array.from(tokens);
4049
const docs = composer.compose(tokensArr, true, text.length);

test/documentSymbols.test.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,8 +273,7 @@ describe('Document Symbols Tests', () => {
273273
? *root
274274
:
275275
style:
276-
height: 41
277-
`;
276+
height: 41`;
278277

279278
const symbols = parseHierarchicalSetup(content);
280279

test/hover.test.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,31 @@ storage:
426426
);
427427
});
428428

429+
it('Hover on property next value on null', async () => {
430+
languageService.addSchema(SCHEMA_ID, {
431+
type: 'object',
432+
properties: {
433+
childObject: {
434+
type: 'object',
435+
description: 'childObject description',
436+
properties: {
437+
prop: {
438+
type: 'string',
439+
description: 'should return this description',
440+
},
441+
},
442+
},
443+
},
444+
});
445+
const content = 'childObject:\r\n prop:\r\n ';
446+
const result = await parseSetup(content, 16);
447+
assert.strictEqual(MarkupContent.is(result.contents), true);
448+
assert.strictEqual(
449+
(result.contents as MarkupContent).value,
450+
`should return this description\n\nSource: [${SCHEMA_ID}](file:///${SCHEMA_ID})`
451+
);
452+
});
453+
429454
it('should work with bad schema', async () => {
430455
const doc = setupSchemaIDTextDocument('foo:\n bar', 'bad-schema.yaml');
431456
yamlSettings.documents = new TextDocumentTestManager();

0 commit comments

Comments
 (0)