@@ -35,8 +35,7 @@ require('script-loader!markdownlint');
35
35
to : CodeMirror . Pos ( lineNumber , end ) ,
36
36
__ruleNames : ruleNames ,
37
37
__ruleDescription : ruleDescription ,
38
- __error : error ,
39
- __lineNumber : lineNumber
38
+ __error : error
40
39
}
41
40
} )
42
41
}
@@ -55,34 +54,30 @@ export const linterOptions = {
55
54
content : `Fix ${ error . ruleDescription } ` ,
56
55
onClick ( ) {
57
56
const doc = window . editor . doc
58
- const fixInfo = error . fixInfo
57
+ const fixInfo = normalizeFixInfo ( error . fixInfo , error . lineNumber )
59
58
const line = fixInfo . lineNumber - 1
60
59
const lineContent = doc . getLine ( line ) || ''
61
- const fixedText = helpers . applyFix ( lineContent , error . fixInfo , '\n' )
60
+ const fixedText = helpers . applyFix ( lineContent , fixInfo , '\n' )
62
61
63
62
let from = { line, ch : 0 }
64
- let to = { line, ch : lineContent ? lineContent . length - 1 : 0 }
63
+ let to = { line, ch : lineContent ? lineContent . length : 0 }
65
64
66
65
if ( typeof fixedText === 'string' ) {
67
66
doc . replaceRange ( fixedText , from , to )
68
67
} else {
69
68
if ( fixInfo . lineNumber === 1 ) {
70
- if ( document . lineCount > 1 ) {
71
- const nextLine = doc . getLine ( to . line + 1 ) || ''
72
- to = {
73
- line : nextLine ,
69
+ if ( doc . lineCount ( ) > 1 ) {
70
+ const nextLineStart = doc . indexFromPos ( {
71
+ line : to . line + 1 ,
74
72
ch : 0
75
- }
73
+ } )
74
+ to = doc . posFromIndex ( nextLineStart )
76
75
}
77
76
} else {
78
- const previousLine = doc . getLine ( from . line - 1 ) || ''
79
- from = {
80
- line : previousLine ,
81
- ch : previousLine . length
82
- }
77
+ const previousLineEnd = doc . indexFromPos ( from ) - 1
78
+ from = doc . posFromIndex ( previousLineEnd )
83
79
}
84
80
85
- // !FIXME: certain range out of bound
86
81
doc . replaceRange ( '' , from , to )
87
82
}
88
83
}
@@ -102,3 +97,20 @@ function lint (content) {
102
97
} )
103
98
return errors
104
99
}
100
+
101
+ // Taken from https://github.com/DavidAnson/markdownlint/blob/2a9274ece586514ba3e2819cec3eb74312dc1b84/helpers/helpers.js#L611
102
+ /**
103
+ * Normalizes the fields of a RuleOnErrorFixInfo instance.
104
+ *
105
+ * @param {Object } fixInfo RuleOnErrorFixInfo instance.
106
+ * @param {number } [lineNumber] Line number.
107
+ * @returns {Object } Normalized RuleOnErrorFixInfo instance.
108
+ */
109
+ function normalizeFixInfo ( fixInfo , lineNumber ) {
110
+ return {
111
+ lineNumber : fixInfo . lineNumber || lineNumber ,
112
+ editColumn : fixInfo . editColumn || 1 ,
113
+ deleteCount : fixInfo . deleteCount || 0 ,
114
+ insertText : fixInfo . insertText || ''
115
+ }
116
+ }
0 commit comments