@@ -32,33 +32,75 @@ export default class CodeActionProvider {
3232 }
3333 }
3434
35- findTables ( ast : Markdoc . Node , params : LSP . CodeActionParams ) : LSP . CodeAction [ ] {
35+ async inlinePartial ( uri : string , line : number , file : string ) : Promise < LSP . WorkspaceEdit | void > {
36+ const ast = this . services . Documents . ast ( uri ) ;
37+ if ( ! ast ) return ;
38+
39+ for ( const node of ast . walk ( ) ) {
40+ if ( node . tag === 'partial' && node . lines [ 0 ] === line && ! node . attributes . variables ) {
41+ const fullPath = this . services . Scanner . fullPath ( file ) ;
42+ const newText = await this . services . Scanner . read ( fullPath ) ;
43+ const [ start , end ] = node . lines ;
44+ const range = LSP . Range . create ( start , 0 , end , 0 ) ;
45+ return { changes : { [ uri ] : [ { range, newText} ] } } ;
46+ }
47+ }
48+ }
49+
50+ findActions ( ast : Markdoc . Node , params : LSP . CodeActionParams ) : LSP . CodeAction [ ] {
3651 const output : LSP . CodeAction [ ] = [ ] ;
3752 const { line} = params . range . start ;
3853 const { uri} = params . textDocument ;
54+
55+ if ( params . range . end . line - params . range . start . line > 3 )
56+ output . push ( {
57+ title : 'Extract content to new partial' ,
58+ command : LSP . Command . create ( 'Extract Partial' , 'markdoc.extractPartial' )
59+ } ) ;
3960
40- for ( const node of ast . walk ( ) )
41- if ( node . type === 'table' && node . lines . includes ( line ) )
61+ for ( const node of ast . walk ( ) ) {
62+ if ( node . type === 'table' && node . lines . includes ( line ) ) {
4263 output . push ( {
4364 data : { type : 'convertTable' , uri, line} ,
4465 title : 'Convert to Markdoc Table' ,
4566 } ) ;
4667
68+ continue ;
69+ }
70+
71+ if ( node . tag === 'partial' && node . lines [ 0 ] === line && ! node . attributes . variables ) {
72+ output . push ( {
73+ data : { type : 'inlinePartial' , uri, line, file : node . attributes . file } ,
74+ title : 'Inline contents of this partial' ,
75+ } ) ;
76+
77+ continue ;
78+ }
79+ }
80+
4781 return output ;
4882 }
4983
5084 onCodeAction ( params : LSP . CodeActionParams ) : ( LSP . CodeAction | LSP . Command ) [ ] {
5185 const ast = this . services . Documents . ast ( params . textDocument . uri ) ;
52- return ast ? this . findTables ( ast , params ) : [ ] ;
86+ return ast ? this . findActions ( ast , params ) : [ ] ;
5387 }
5488
55- onCodeActionResolve ( action : LSP . CodeAction ) : LSP . CodeAction {
89+ async onCodeActionResolve ( action : LSP . CodeAction ) : Promise < LSP . CodeAction > {
90+ if ( ! action . data ?. type ) return action ;
91+
5692 if ( action . data . type === 'convertTable' ) {
5793 const { uri, line} = action . data ;
5894 const edit = this . convertTable ( uri , line ) ;
5995 if ( edit ) return { ...action , edit} ;
6096 }
6197
98+ if ( action . data . type === 'inlinePartial' ) {
99+ const { uri, line, file} = action . data ;
100+ const edit = await this . inlinePartial ( uri , line , file ) ;
101+ if ( edit ) return { ...action , edit} ;
102+ }
103+
62104 return action ;
63105 }
64106}
0 commit comments