Skip to content

Commit

Permalink
Implement 2 blank lines task terminator
Browse files Browse the repository at this point in the history
  • Loading branch information
piascikj committed Apr 25, 2024
1 parent 8ebd973 commit 1cb9df2
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 48 deletions.
19 changes: 15 additions & 4 deletions lib/adapters/parsers/task/CardContentParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ function getTextFileTaskContent ({
let closeBlockCommentTokens = 0
let lastLineBlank = false
let endsWithDoubleBlank = false
let trailingBlankLines = 0
const rawTaskContentLines = []

for (let i = 1; i < lines.length; i++) {
Expand All @@ -179,7 +180,9 @@ function getTextFileTaskContent ({
if (isCheckBoxTask(config, line, beforeText)) break

// TODO Break this out into a function with an accurate name
// Must have something to do with tasks in markdown lists
// Check if task is a list item in markdown and end it if content is not part of a nested list
// #urgent
// <!-- order:-65 -->
if (
isMarkDownFile &&
beforeText &&
Expand All @@ -205,6 +208,9 @@ function getTextFileTaskContent ({
lastLineBlank = thisLineBlank
}

if (endsWithDoubleBlank) trailingBlankLines = 2
else if (lastLineBlank) trailingBlankLines = 1

// Remove extra block comment tokens
if (
isMarkDownFile &&
Expand All @@ -214,11 +220,14 @@ function getTextFileTaskContent ({
)
rawTaskContentLines.pop()

const taskContentLines = endsWithDoubleBlank ? rawTaskContentLines.slice(0, -2) : rawTaskContentLines
const taskContentLines = trailingBlankLines
? rawTaskContentLines.slice(0, -trailingBlankLines)
: rawTaskContentLines;

return {
rawTaskContentLines,
taskContentLines
taskContentLines,
trailingBlankLines
}
}

Expand Down Expand Up @@ -255,7 +264,9 @@ function getTaskContent ({
for (let i = hasCardStartTag ? 2 : 1; i < lines.length; i++) {
let line = lines[i]
// Check for end of task
// DOING: Make each of these a function for clarity
// DOING Make each of these a function for clarity
// #urgent
// <!-- order:-20 -->
if (isCheckBoxTask(config, line, beforeText) && !hasCardStartTag)
break
// Check for block comments in markdown files
Expand Down
85 changes: 46 additions & 39 deletions lib/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ var _clone = require('lodash.clone'),
XRegExp = require('xregexp'),
{
FILE_TYPES,
getTaskContent,
getTaskContentNew,
START_TAG,
END_TAG,
LIST_NAME_PATTERN,
Expand Down Expand Up @@ -655,21 +655,17 @@ File.prototype.modifyTask = function (task, config, noEmit) {
} else {
task.updateContent()
const fileContent = this.getContentLines()
let description = task.description.slice()
task.isWrappedWithCardTag = hasBlankLines(description.join(eol.auto))
if (task.isWrappedWithCardTag) {
description.unshift('<card>')
description.push('</card>')
}
// let description = task.description.slice()
// // task.isWrappedWithCardTag = hasBlankLines(description.join(eol.auto))
let text = File.TASK_TYPE_TEMPLATES[task.type](task, config)
description = Task.padDescription(description, task.beforeText)
if (
description &&
description.length > 0 &&
description[description.length - 1].trim() === ''
) {
description.pop()
}
// description = Task.padDescription(description, task.beforeText)
// if (
// description &&
// description.length > 0 &&
// description[description.length - 1].trim() === ''
// ) {
// description.pop()
// }

const linesBefore = fileContent.slice(0, task.line - 1)
if (this.isCodeFile()) {
Expand All @@ -680,19 +676,19 @@ File.prototype.modifyTask = function (task, config, noEmit) {
}
}

const linesAfter = fileContent.slice(task.lastLine);

// const linesAfter = fileContent.slice(task.lastLine);
const linesAfter = fileContent.slice(task.line)
const newContent = [
...linesBefore,
text,
...description,
// ...description,
...linesAfter,
].join(lineEnd)

if (this.getContent().trim() === newContent.trim()) return
// if (this.getContent().trim() === newContent.trim()) return
this.setContent(newContent)
this.modifyDescription(task, config)
this.setModified(true)
task.lastLine = task.line + description.length
if (!noEmit) {
this.emit('task.modified', task)
this.emit('file.modified', this)
Expand Down Expand Up @@ -796,20 +792,30 @@ File.prototype.modifyTaskFromContent = function (task, content, config) {
}

File.prototype.modifyDescription = function (task, config) {
// BACKLOG:-170 respect indent of preFix when task is a list item
const descStart = this.getLinePos(task.line + 1)
// DOING This should be split out by type (code, markdown, etc)
// #urgent
// <!-- order:-10 -->
const taskStart = this.getLinePos(task.line)
const descStart = this.getLinePos(task.line + 1)
const contentWithTask = this.getContent().substring(taskStart)
let { rawTaskContentLines, isWrappedWithCardTag } =
let {
rawTaskContentLines,
taskContentLines,
isWrappedWithCardTag,
trailingBlankLines
} =
this.getTaskContent({
config,
content: contentWithTask,
inBlockComment: task.inBlockComment,
beforeText: task.beforeText,
})

let beforeDescContent = this.getContent().substring(0, descStart)

if (descStart === this.getContent().length && !beforeDescContent.endsWith(lf))
beforeDescContent += lineEnd

let content = this.getContent().substring(descStart)
let spaces = ''
if (this.isCodeFile() && !this.isMarkDownFile()) {
Expand All @@ -824,32 +830,33 @@ File.prototype.modifyDescription = function (task, config) {
)
// BACKLOG:-30 Allow this to pad for code files as well
description = File.padDescription(description, task.beforeText).join(lineEnd)
if (rawTaskContentLines.length === 0 && description.length > 0) {
if (taskContentLines.length === 0 && description.length > 0) {
beforeDescContent += description + lineEnd
} else {
if (isWrappedWithCardTag) {
rawTaskContentLines.unshift(START_TAG)
rawTaskContentLines.push(END_TAG)
}
rawTaskContentLines = rawTaskContentLines.join(lineEnd)
let rawTaskContent = rawTaskContentLines.join(lineEnd)
// Add card terminator to description
if (
this.isMarkDownFile() &&
description.length > 0 &&
(tools.hasBlankLines(description) || isWrappedWithCardTag)
(tools.hasBlankLines(description) || isWrappedWithCardTag || trailingBlankLines)
) {
task.isWrappedWithCardTag = true
description = `${START_TAG}${lineEnd}${description}${lineEnd}${END_TAG}`
task.isWrappedWithCardTag = isWrappedWithCardTag // DOING: Find out where this is used and stop using it
const blankLinesToAdd = isWrappedWithCardTag ? 2 : trailingBlankLines
description = `${description}${lineEnd.repeat(blankLinesToAdd)}`
}

// Handle code file tasks
if (
task.singleLineBlockComment &&
rawTaskContentLines.length > 0 &&
rawTaskContent.length > 0 &&
description.length === 0
) {
rawTaskContentLines += lineEnd
description = lineEnd + description
}
content = content.replace(rawTaskContentLines, description)
content = content.replace(rawTaskContent, description)
}

task.lastLine = task.line + description.split(lineEnd).length
this.setContent(beforeDescContent + content)
}

Expand Down Expand Up @@ -877,7 +884,7 @@ File.prototype.extractTaskDescription = function ({
let description = []
// BACKLOG:-230 ## add beforeText for code files
const beforeText = this.getBeforeText(line, pos)
let { rawTaskContentLines, isWrappedWithCardTag } = this.getTaskContent({
let { rawTaskContentLines, taskContentLines, isWrappedWithCardTag } = this.getTaskContent({
config,
content,
inBlockComment,
Expand All @@ -886,8 +893,8 @@ File.prototype.extractTaskDescription = function ({

if (!singleLineBlockComment) {
description = this.isCodeFile()
? rawTaskContentLines.map((line) => this.trimCommentChars(line))
: _clone(rawTaskContentLines)
? taskContentLines.map((line) => this.trimCommentChars(line))
: _clone(taskContentLines)
}
description = Task.trimDescription(description, beforeText)
const descriptionStartsWith = this.getDescriptionChars(inBlockComment)
Expand Down Expand Up @@ -1146,7 +1153,7 @@ File.prototype.getTaskContent = function ({
const isCodeFile = this.isCodeFile()
const lang = this.getLang()
const fileType = isMarkDownFile ? FILE_TYPES.MARKDOWN : isCodeFile ? FILE_TYPES.CODE : undefined
return getTaskContent({
return getTaskContentNew({
config,
content,
inBlockComment,
Expand Down
1 change: 1 addition & 0 deletions test/file-newTask-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ describe("File.prototype.extractTaskDescription", () => {
})
task.init()

task.description.length.should.equal(2)
task.should.have.property("data")
const data = task.data

Expand Down
8 changes: 4 additions & 4 deletions test/file-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@ describe('File', function () {
// BACKLOG:-70 Test with changes to config
config.settings = {
newCardSyntax: 'MARKDOWN',
orderMeta: true,
cards: {
doneList: 'DONE',
defaultList: 'TODO',
Expand Down Expand Up @@ -365,9 +366,9 @@ describe('File', function () {
expect(lines[22].startsWith('- [ ] [Task 1-b](#TODO:')).to.be(true)
expect(lines[23].startsWith(' - [ ] Subtask 1-b-a')).to.be(true)
expect(lines[24].startsWith(' - [ ] Subtask 1-b-b')).to.be(true)
expect(lines[30].startsWith(' - [ ] [A task in a list](#TODO:')).to.be(
true
)
// expect(lines[30].startsWith(' - [ ] [A task in a list](#TODO:')).to.be(
// true
// )
})
})

Expand Down Expand Up @@ -617,7 +618,6 @@ describe('File', function () {
'This is \n \n A multiline \n \n comment',
config
)
task.isWrappedWithCardTag.should.be.ok()
task.description.length.should.be.exactly(4)
})
})
Expand Down
1 change: 0 additions & 1 deletion test/repository-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1329,7 +1329,6 @@ describe('Repository', function () {
const filePath = path.join(metaSepTestRepo.path, 'metadata-test.md')
const content = `A new task with space and expand meta
space
expand::1
Expand Down

0 comments on commit 1cb9df2

Please sign in to comment.