mirror of https://github.com/status-im/codimd.git
Fix invalid range
Signed-off-by: Yukai Huang <yukaihuangtw@gmail.com>
This commit is contained in:
parent
7caa272175
commit
f07e3f10b6
|
@ -35,8 +35,7 @@ require('script-loader!markdownlint');
|
||||||
to: CodeMirror.Pos(lineNumber, end),
|
to: CodeMirror.Pos(lineNumber, end),
|
||||||
__ruleNames: ruleNames,
|
__ruleNames: ruleNames,
|
||||||
__ruleDescription: ruleDescription,
|
__ruleDescription: ruleDescription,
|
||||||
__error: error,
|
__error: error
|
||||||
__lineNumber: lineNumber
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -55,34 +54,30 @@ export const linterOptions = {
|
||||||
content: `Fix ${error.ruleDescription}`,
|
content: `Fix ${error.ruleDescription}`,
|
||||||
onClick () {
|
onClick () {
|
||||||
const doc = window.editor.doc
|
const doc = window.editor.doc
|
||||||
const fixInfo = error.fixInfo
|
const fixInfo = normalizeFixInfo(error.fixInfo, error.lineNumber)
|
||||||
const line = fixInfo.lineNumber - 1
|
const line = fixInfo.lineNumber - 1
|
||||||
const lineContent = doc.getLine(line) || ''
|
const lineContent = doc.getLine(line) || ''
|
||||||
const fixedText = helpers.applyFix(lineContent, error.fixInfo, '\n')
|
const fixedText = helpers.applyFix(lineContent, fixInfo, '\n')
|
||||||
|
|
||||||
let from = { line, ch: 0 }
|
let from = { line, ch: 0 }
|
||||||
let to = { line, ch: lineContent ? lineContent.length - 1 : 0 }
|
let to = { line, ch: lineContent ? lineContent.length : 0 }
|
||||||
|
|
||||||
if (typeof fixedText === 'string') {
|
if (typeof fixedText === 'string') {
|
||||||
doc.replaceRange(fixedText, from, to)
|
doc.replaceRange(fixedText, from, to)
|
||||||
} else {
|
} else {
|
||||||
if (fixInfo.lineNumber === 1) {
|
if (fixInfo.lineNumber === 1) {
|
||||||
if (document.lineCount > 1) {
|
if (doc.lineCount() > 1) {
|
||||||
const nextLine = doc.getLine(to.line + 1) || ''
|
const nextLineStart = doc.indexFromPos({
|
||||||
to = {
|
line: to.line + 1,
|
||||||
line: nextLine,
|
|
||||||
ch: 0
|
ch: 0
|
||||||
}
|
})
|
||||||
|
to = doc.posFromIndex(nextLineStart)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
const previousLine = doc.getLine(from.line - 1) || ''
|
const previousLineEnd = doc.indexFromPos(from) - 1
|
||||||
from = {
|
from = doc.posFromIndex(previousLineEnd)
|
||||||
line: previousLine,
|
|
||||||
ch: previousLine.length
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// !FIXME: certain range out of bound
|
|
||||||
doc.replaceRange('', from, to)
|
doc.replaceRange('', from, to)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -102,3 +97,20 @@ function lint (content) {
|
||||||
})
|
})
|
||||||
return errors
|
return errors
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Taken from https://github.com/DavidAnson/markdownlint/blob/2a9274ece586514ba3e2819cec3eb74312dc1b84/helpers/helpers.js#L611
|
||||||
|
/**
|
||||||
|
* Normalizes the fields of a RuleOnErrorFixInfo instance.
|
||||||
|
*
|
||||||
|
* @param {Object} fixInfo RuleOnErrorFixInfo instance.
|
||||||
|
* @param {number} [lineNumber] Line number.
|
||||||
|
* @returns {Object} Normalized RuleOnErrorFixInfo instance.
|
||||||
|
*/
|
||||||
|
function normalizeFixInfo (fixInfo, lineNumber) {
|
||||||
|
return {
|
||||||
|
lineNumber: fixInfo.lineNumber || lineNumber,
|
||||||
|
editColumn: fixInfo.editColumn || 1,
|
||||||
|
deleteCount: fixInfo.deleteCount || 0,
|
||||||
|
insertText: fixInfo.insertText || ''
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue