Skip to content

Commit

Permalink
Merge pull request #3166 from obsidian-tasks-group/fix-continuation-o…
Browse files Browse the repository at this point in the history
…n-last-line

fix: '\' at end of last line in query no longer discards the instruction

Fixes #3137
  • Loading branch information
claremacrae authored Nov 2, 2024
2 parents e1a8a1c + 5cb0e01 commit bd7d842
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/Query/Scanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,15 @@ export function continueLines(input: string): Statement[] {

let currentStatementRaw = '';
let currentStatementProcessed = '';
for (const inputLine of input.split('\n')) {

// See https://github.com/obsidian-tasks-group/obsidian-tasks/issues/3137.
// Issue #3137 revealed that if the last line of the query ended in
// a backslash, this function returned without saved the final
// instruction.
// The simplest way to prevent this is to add an extra end-of-line
// to the end of the query, which will get ignored when not needed:
const inputWithGuaranteedFinalEOL = input + '\n';
for (const inputLine of inputWithGuaranteedFinalEOL.split('\n')) {
const adjustedLine = adjustLine(inputLine, continuePreviousLine);
if (continuePreviousLine) {
currentStatementRaw += '\n' + inputLine;
Expand Down
6 changes: 6 additions & 0 deletions tests/Query/Query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,12 @@ describe('Query parsing', () => {
expect(new Query('short mode\nfull').queryLayoutOptions.shortMode).toEqual(false);
});

it('should cope with missing end-of-line character in query ending with continuation character - #3137', () => {
const query = new Query('path includes query.md \\');
expect(query.filters.length).toEqual(1);
expect(query.filters[0].instruction).toEqual('path includes query.md');
});

describe('should include instruction in parsing error messages', () => {
function getQueryError(source: string) {
return new Query(source, new TasksFile('Example Path.md')).error;
Expand Down
5 changes: 5 additions & 0 deletions tests/Query/Scanner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,11 @@ describe('continue_lines', () => {
expect(continueLinesFlattened(text)).toEqual('description includes one two three four five six');
});

it('should preserve last instruction in query that ends in a continuation line = #3137', () => {
const text = 'due today \\';
expect(continueLinesFlattened(text)).toEqual('due today');
});

it('visualise continue_lines', () => {
const output = `
input:
Expand Down

0 comments on commit bd7d842

Please sign in to comment.