11import * as vscode from "vscode" ;
22import { execFeed , extensionConfiguration , getOutputChannel , mesonProgram } from "../utils" ;
3- import { Tool , ToolCheckResult } from "../types" ;
3+ import { Tool , CheckResult } from "../types" ;
44import { getMesonVersion } from "../introspection" ;
55import { Version } from "../version" ;
66
7- export async function format ( meson : Tool , root : string , document : vscode . TextDocument ) : Promise < vscode . TextEdit [ ] > {
7+ export interface MesonOptions {
8+ supportsFileNameArgument : boolean ;
9+ }
10+
11+ export type MesonTool = Tool < MesonOptions > ;
12+
13+ export async function format (
14+ meson : MesonTool ,
15+ root : string ,
16+ document : vscode . TextDocument ,
17+ ) : Promise < vscode . TextEdit [ ] > {
818 const originalDocumentText = document . getText ( ) ;
919
1020 let args = [ "format" ] ;
@@ -15,6 +25,10 @@ export async function format(meson: Tool, root: string, document: vscode.TextDoc
1525 }
1626 args . push ( "-" ) ;
1727
28+ if ( meson . options . supportsFileNameArgument ) {
29+ args . push ( "--source-file-path" , document . fileName ) ;
30+ }
31+
1832 const { stdout, stderr, error } = await execFeed ( meson . path , args , { cwd : root } , originalDocumentText ) ;
1933 if ( error ) {
2034 //TODO: file a bug report, meson prints some errors on stdout :(
@@ -35,8 +49,9 @@ export async function format(meson: Tool, root: string, document: vscode.TextDoc
3549
3650const formattingSupportedSinceVersion = new Version ( [ 1 , 5 , 0 ] ) ;
3751const formattingWithStdinSupportedSinceVersion = new Version ( [ 1 , 7 , 0 ] ) ;
52+ const formattingWithFileNameArgumentSinceVersion = new Version ( [ 1 , 9 , 0 ] ) ;
3853
39- export async function check ( ) : Promise < ToolCheckResult > {
54+ export async function check ( ) : Promise < CheckResult < MesonTool > > {
4055 const meson_path = mesonProgram ( ) ;
4156
4257 let mesonVersion ;
@@ -45,23 +60,29 @@ export async function check(): Promise<ToolCheckResult> {
4560 } catch ( e ) {
4661 const error = e as Error ;
4762 console . log ( error ) ;
48- return ToolCheckResult . newError ( error . message ) ;
63+ return CheckResult . newError < MesonTool > ( error . message ) ;
4964 }
5065
5166 // meson format was introduced in 1.5.0
5267 // see https://mesonbuild.com/Commands.html#format
5368 if ( mesonVersion . compareWithOther ( formattingSupportedSinceVersion ) < 0 ) {
54- ToolCheckResult . newError (
69+ CheckResult . newError < MesonTool > (
5570 `Meson supports formatting only since version ${ formattingSupportedSinceVersion } , but you have version ${ mesonVersion } ` ,
5671 ) ;
5772 }
5873
5974 // using "-" as stdin is only supported since 1.7.0 (see https://github.com/mesonbuild/meson/pull/13793)
6075 if ( mesonVersion . compareWithOther ( formattingWithStdinSupportedSinceVersion ) < 0 ) {
61- return ToolCheckResult . newError (
76+ return CheckResult . newError < MesonTool > (
6277 `Meson supports formatting from stdin only since version ${ formattingWithStdinSupportedSinceVersion } , but you have version ${ mesonVersion } ` ,
6378 ) ;
6479 }
6580
66- return ToolCheckResult . newTool ( { path : meson_path , version : mesonVersion } ) ;
81+ const supportsFileNameArgument = mesonVersion . compareWithOther ( formattingWithFileNameArgumentSinceVersion ) >= 0 ;
82+
83+ const options : MesonOptions = {
84+ supportsFileNameArgument,
85+ } ;
86+
87+ return CheckResult . newData < MesonTool > ( { path : meson_path , version : mesonVersion , options } ) ;
6788}
0 commit comments