Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add meson as formatter #261

Merged
merged 5 commits into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Change Log

# 1.26.0
## next

- Add support for meson as formatter

## 1.26.0

- Bump mesonlsp to v4.3.3
- Fix debug providers
Expand Down
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ the [Material Design Icons](https://materialdesignicons.com/) project.
introspection files:
`"C_Cpp.default.configurationProvider": "mesonbuild.mesonbuild"`

\* - requires an installation of [muon](https://muon.build).

## New Extension ID

If you come from a previous installation, please make sure you are on the
Expand All @@ -41,4 +39,4 @@ extension on the store, and only that one is released from this repository.
[mesonlsp](https://github.com/JCWasmx86/mesonlsp) or
[muon](https://muon.build).

[^1]: Requires an installation of muon.
[^1]: Requires an installation of muon or meson >= 1.5.0
17 changes: 16 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,8 @@
"type": "string",
"default": "muon",
"enum": [
"muon"
"muon",
"meson"
],
"description": "Select which formatting provider to use"
},
Expand All @@ -203,6 +204,11 @@
"default": null,
"description": "Path to muon formatter config.ini"
},
"mesonbuild.formatting.mesonConfig": {
"type": "string",
"default": null,
"description": "Path to meson format config 'meson.format'"
},
"mesonbuild.debugOptions": {
"type": "object",
"default": {
Expand Down Expand Up @@ -428,6 +434,15 @@
"aliases": [
"Meson Wrap"
]
},
{
"id": "ini",
"filenamePatterns": [
"meson.format"
],
"aliases": [
"Meson Format Config"
]
}
],
"grammars": [
Expand Down
9 changes: 7 additions & 2 deletions src/formatters.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import * as vscode from "vscode";
import { extensionConfiguration, getOutputChannel } from "./utils";
import { ToolCheckFunc, Tool } from "./types";
import { ToolCheckFunc, Tool, type FormattingProvider } from "./types";
import * as muon from "./tools/muon";
import * as meson from "./tools/meson";

type FormatterFunc = (tool: Tool, root: string, document: vscode.TextDocument) => Promise<vscode.TextEdit[]>;

Expand All @@ -10,11 +11,15 @@ type FormatterDefinition = {
check: ToolCheckFunc;
};

const formatters: Record<string, FormatterDefinition> = {
const formatters: Record<FormattingProvider, FormatterDefinition> = {
muon: {
format: muon.format,
check: muon.check,
},
meson: {
format: meson.format,
check: meson.check,
},
};

async function reloadFormatters(sourceRoot: string, context: vscode.ExtensionContext): Promise<vscode.Disposable[]> {
Expand Down
67 changes: 67 additions & 0 deletions src/tools/meson.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import * as vscode from "vscode";
import { execFeed, extensionConfiguration, getOutputChannel, mesonProgram } from "../utils";
import { Tool, ToolCheckResult } from "../types";
import { getMesonVersion } from "../introspection";
import { Version } from "../version";

export async function format(meson: Tool, root: string, document: vscode.TextDocument): Promise<vscode.TextEdit[]> {
const originalDocumentText = document.getText();

let args = ["format"];

const config_path = extensionConfiguration("formatting").mesonConfig;
if (config_path) {
args.push("-c", config_path);
}
args.push("-");

const { stdout, stderr, error } = await execFeed(meson.path, args, { cwd: root }, originalDocumentText);
if (error) {
//TODO: file a bug report, meson prints some errors on stdout :(
const errorString = stderr.trim().length > 0 ? stderr : stdout;

getOutputChannel().appendLine(`Failed to format document with meson: ${errorString}`);
getOutputChannel().show(true);
return [];
}

const documentRange = new vscode.Range(
document.lineAt(0).range.start,
document.lineAt(document.lineCount - 1).rangeIncludingLineBreak.end,
);

return [new vscode.TextEdit(documentRange, stdout)];
}

const formattingSupportedSinceVersion = new Version([1, 5, 0]);
const formattingWithStdinSupportedSinceVersion = new Version([1, 7, 0]);

export async function check(): Promise<ToolCheckResult> {
const meson_path = mesonProgram();

let mesonVersion;
try {
mesonVersion = await getMesonVersion();
} catch (e) {
const error = e as Error;
console.log(error);
return ToolCheckResult.newError(error.message);
}

// meson format was introduced in 1.5.0
// see https://mesonbuild.com/Commands.html#format
if (mesonVersion.compareWithOther(formattingSupportedSinceVersion) < 0) {
ToolCheckResult.newError(
`Meson supports formatting only since version ${formattingSupportedSinceVersion}, but you have version ${mesonVersion}`,
);
}

// using "-" as stdin is only supported since 1.7.0 (see https://github.com/mesonbuild/meson/pull/13793)
if (mesonVersion.compareWithOther(formattingWithStdinSupportedSinceVersion) < 0) {
return ToolCheckResult.newError(
`Meson supports formatting from stdin only since version ${formattingWithStdinSupportedSinceVersion}, but you have version ${mesonVersion}`,
);
}

return ToolCheckResult.newTool({ path: meson_path, version: mesonVersion });
}
5 changes: 4 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ export type LinterConfiguration = {
export type LanguageServer = "Swift-MesonLSP" | "mesonlsp" | null;
export type ModifiableExtension = "ms-vscode.cpptools" | "rust-lang.rust-analyzer";

export type FormattingProvider = "muon" | "meson";

export interface ExtensionConfiguration {
configureOnOpen: boolean | "ask";
configureOptions: string[];
Expand All @@ -80,8 +82,9 @@ export interface ExtensionConfiguration {
};
formatting: {
enabled: boolean;
provider: "muon";
provider: FormattingProvider;
muonConfig: string | null;
mesonConfig: string | null;
};
debugOptions: object;
languageServer: LanguageServer;
Expand Down
Loading