Skip to content

Commit

Permalink
feat: Add CSS support via malva
Browse files Browse the repository at this point in the history
  • Loading branch information
magic-akari committed Oct 27, 2023
1 parent ca5dac0 commit 0e9286f
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 5 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ All notable changes to the "wasm-fmt" extension will be documented in this file.

Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how to structure this file.

## [0.3.0]
Introduce new formatter: malva

CSS/SCSS/SASS/LESS are supported.

## [0.2.3]
tune c/cpp default style

Expand Down
9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,22 @@
"onLanguage:c",
"onLanguage:cpp",
"onLanguage:csharp",
"onLanguage:css",
"onLanguage:go",
"onLanguage:java",
"onLanguage:javascript",
"onLanguage:javascriptreact",
"onLanguage:json",
"onLanguage:jsonc",
"onLanguage:less",
"onLanguage:objective-c",
"onLanguage:objective-cpp",
"onLanguage:proto",
"onLanguage:python",
"onLanguage:sass",
"onLanguage:scss",
"onLanguage:typescript",
"onLanguage:typescriptreact",
"onLanguage:proto",
"onLanguage:zig"
],
"browser": "./out/extension.js",
Expand Down Expand Up @@ -67,7 +71,8 @@
"@wasm-fmt/clang-format": "^0.2.7",
"@wasm-fmt/gofmt": "^0.4.5",
"@wasm-fmt/ruff_fmt": "^0.4.3",
"@wasm-fmt/zig_fmt": "^0.0.3"
"@wasm-fmt/zig_fmt": "^0.0.3",
"malva": "^0.1.1"
},
"keywords": [
"prettier",
Expand Down
7 changes: 7 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions src/env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,9 @@ declare module "@wasm-fmt/*.wasm" {
const wasm: string;
export default wasm;
}

declare module "malva";
declare module "malva/standalone_wasm_bg.wasm" {
const wasm: string;
export default wasm;
}
1 change: 0 additions & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Import the module and reference it with the alias vscode in your code below
import * as vscode from "vscode";
import init, { formattingSubscription } from "./format";
import "./format/ruff_fmt";

// This method is called when your extension is activated
// Your extension is activated the very first time the command is executed
Expand Down
7 changes: 5 additions & 2 deletions src/format/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,28 @@ import clang_init, {
formattingSubscription as clang_sub,
} from "./clang-format";
import go_init, { formattingSubscription as go_sub } from "./gofmt";
import malva_init, { formattingSubscription as malva_sub } from "./malva_fmt";
import ruff_init, { formattingSubscription as ruff_sub } from "./ruff_fmt";
import zig_init, { formattingSubscription as zig_sub } from "./zig_fmt";

export default function init(context: vscode.ExtensionContext) {
return Promise.all([
biome_init(context),
clang_init(context),
go_init(context),
malva_init(context),
ruff_init(context),
clang_init(context),
zig_init(context),
]);
}

export function formattingSubscription() {
return vscode.Disposable.from(
biome_sub(),
clang_sub(),
go_sub(),
malva_sub(),
ruff_sub(),
clang_sub(),
zig_sub(),
);
}
45 changes: 45 additions & 0 deletions src/format/malva_fmt.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { format as malva_fmt, initSync as malva_init } from "malva";
import malva_wasm from "malva/standalone_wasm_bg.wasm";
import * as vscode from "vscode";
import { Logger } from "../logger";

const logger = new Logger("malva-format");

export default async function init(context: vscode.ExtensionContext) {
const wasm_uri = vscode.Uri.joinPath(context.extensionUri, malva_wasm);

const bits = await vscode.workspace.fs.readFile(wasm_uri);
malva_init(bits);
}

export function formattingSubscription() {
return vscode.languages.registerDocumentFormattingEditProvider(
["css", "scss", "sass", "less"],
{
provideDocumentFormattingEdits(document, options, token) {
const text = document.getText();

const useTabs = !options.insertSpaces;
const indentWidth = options.tabSize;

try {
const formatted = malva_fmt(text, document.languageId, {
useTabs,
indentWidth,
});

const range = document.validateRange(
new vscode.Range(
document.positionAt(0),
document.positionAt(text.length)
)
);
return [vscode.TextEdit.replace(range, formatted)];
} catch (error) {
logger.error(error);
return [];
}
},
}
);
}

0 comments on commit 0e9286f

Please sign in to comment.