@@ -7,6 +7,8 @@ import type {
7
7
import { ButtonActionType } from 'packages/core/src/config/ButtonConfig' ;
8
8
import { AbstractButtonActionConfig } from 'packages/core/src/fields/button/AbstractButtonActionConfig' ;
9
9
import type { IPlugin } from 'packages/core/src/IPlugin' ;
10
+ import { P_lineNumberExpression } from 'packages/core/src/parsers/nomParsers/MiscNomParsers' ;
11
+ import { runParser } from 'packages/core/src/parsers/ParsingError' ;
10
12
11
13
export class ReplaceInNoteButtonActionConfig extends AbstractButtonActionConfig < ReplaceInNoteButtonAction > {
12
14
constructor ( plugin : IPlugin ) {
@@ -17,31 +19,41 @@ export class ReplaceInNoteButtonActionConfig extends AbstractButtonActionConfig<
17
19
_config : ButtonConfig | undefined ,
18
20
action : ReplaceInNoteButtonAction ,
19
21
filePath : string ,
20
- _context : ButtonContext ,
22
+ context : ButtonContext ,
21
23
_click : ButtonClickContext ,
22
24
) : Promise < void > {
23
- if ( action . fromLine > action . toLine ) {
24
- throw new Error ( 'From line cannot be greater than to line' ) ;
25
- }
26
-
27
25
const replacement = action . templater
28
26
? await this . plugin . internal . evaluateTemplaterTemplate (
29
27
this . plugin . api . buttonActionRunner . resolveFilePath ( action . replacement ) ,
30
28
filePath ,
31
29
)
32
30
: action . replacement ;
33
31
32
+ const fromLine = runParser ( P_lineNumberExpression , action . fromLine . toString ( ) ) ;
33
+ const toLine = runParser ( P_lineNumberExpression , action . toLine . toString ( ) ) ;
34
+
34
35
await this . plugin . internal . file . atomicModify ( filePath , content => {
35
36
let splitContent = content . split ( '\n' ) ;
36
37
37
- if ( action . fromLine < 0 || action . toLine > splitContent . length + 1 ) {
38
- throw new Error ( 'Line numbers out of bounds' ) ;
38
+ const lineContext = this . plugin . api . buttonActionRunner . getLineNumberContext ( content , context . position ) ;
39
+ const fromLineNumber = fromLine . evaluate ( lineContext ) ;
40
+ const toLineNumber = toLine . evaluate ( lineContext ) ;
41
+
42
+ if ( fromLineNumber > toLineNumber ) {
43
+ throw new Error ( `From line (${ fromLineNumber } ) can't be greater than to line (${ toLineNumber } )` ) ;
44
+ }
45
+
46
+ if ( fromLineNumber < 1 ) {
47
+ throw new Error ( `From line (${ fromLineNumber } ) can't smaller than 1.` ) ;
48
+ }
49
+ if ( toLineNumber > splitContent . length ) {
50
+ throw new Error ( `To line (${ toLineNumber } ) can't greater than the file length ${ splitContent . length } .` ) ;
39
51
}
40
52
41
53
splitContent = [
42
- ...splitContent . slice ( 0 , action . fromLine - 1 ) ,
54
+ ...splitContent . slice ( 0 , fromLineNumber - 1 ) ,
43
55
replacement ,
44
- ...splitContent . slice ( action . toLine ) ,
56
+ ...splitContent . slice ( toLineNumber ) ,
45
57
] ;
46
58
47
59
return splitContent . join ( '\n' ) ;
0 commit comments