Skip to content

Commit 7caea0f

Browse files
Update VSCode extension to use stdin-filepath and respect-ignores (#913)
1 parent 5e4e837 commit 7caea0f

File tree

5 files changed

+22
-36
lines changed

5 files changed

+22
-36
lines changed

stylua-vscode/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ To view the changelog of the StyLua binary, see [here](https://github.com/Johnny
1111

1212
## [Unreleased]
1313

14+
### Changed
15+
16+
- The VSCode extension will now defer to using `stylua` itself to determine ignores and other configuration, rather than rolling its own ignore system
17+
- The VSCode extension now passes `--stdin-filepath` and `--respect-ignores` to the command line
18+
1419
## [1.6.3] - 2024-01-06
1520

1621
### Fixed

stylua-vscode/package-lock.json

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

stylua-vscode/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,6 @@
166166
"webpack-cli": "^4.9.2"
167167
},
168168
"dependencies": {
169-
"ignore": "^5.1.8",
170169
"node-fetch": "^2.6.1",
171170
"semver": "^7.5.4",
172171
"unzipper": "^0.10.14",

stylua-vscode/src/extension.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as vscode from "vscode";
22
import * as semver from "semver";
3-
import { formatCode, checkIgnored } from "./stylua";
3+
import { formatCode } from "./stylua";
44
import { GitHub, GitHubRelease } from "./github";
55
import { ResolveMode, StyluaDownloader, StyluaInfo } from "./download";
66
import { getDesiredVersion } from "./util";
@@ -241,16 +241,14 @@ export async function activate(context: vscode.ExtensionContext) {
241241
);
242242
const cwd = currentWorkspace?.uri?.fsPath;
243243

244-
if (await checkIgnored(document.uri, currentWorkspace?.uri)) {
245-
return [];
246-
}
247-
248244
const text = document.getText();
249245

250246
try {
251247
const formattedText = await formatCode(
248+
outputChannel,
252249
styluaBinaryPath.path,
253250
text,
251+
document.uri.fsPath,
254252
cwd,
255253
byteOffset(document, range.start),
256254
byteOffset(document, range.end)

stylua-vscode/src/stylua.ts

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,23 @@
11
import * as vscode from "vscode";
22
import { spawn, exec } from "child_process";
3-
import ignore from "ignore";
4-
import { fileExists } from "./util";
5-
6-
export async function checkIgnored(
7-
filePath?: vscode.Uri,
8-
currentWorkspace?: vscode.Uri
9-
): Promise<boolean> {
10-
if (!filePath || !currentWorkspace) {
11-
return false;
12-
}
13-
14-
const ignoreFilePath = vscode.Uri.joinPath(currentWorkspace, ".styluaignore");
15-
if (await fileExists(ignoreFilePath)) {
16-
try {
17-
const contents = await vscode.workspace.fs.readFile(ignoreFilePath);
18-
const ig = ignore().add(contents.toString());
19-
return ig.ignores(vscode.workspace.asRelativePath(filePath));
20-
} catch (err) {
21-
vscode.window.showErrorMessage(
22-
`Could not read StyLua ignore file at ${ignoreFilePath}:\n${err}`
23-
);
24-
return false;
25-
}
26-
}
27-
28-
return false;
29-
}
303

314
export function formatCode(
5+
outputChannel: vscode.LogOutputChannel,
326
path: string,
337
code: string,
8+
filePath?: string,
349
cwd?: string,
3510
startPos?: number,
3611
endPos?: number
3712
): Promise<string> {
3813
return new Promise((resolve, reject) => {
39-
const args = [];
14+
const args = ["--respect-ignores"];
15+
16+
if (filePath) {
17+
args.push("--stdin-filepath");
18+
args.push(filePath);
19+
}
20+
4021
if (startPos) {
4122
args.push("--range-start");
4223
args.push(startPos.toString());
@@ -65,6 +46,8 @@ export function formatCode(
6546

6647
args.push("-");
6748

49+
outputChannel.debug(`${path} {args.join(" ")}`);
50+
6851
const child = spawn(`${path}`, args, {
6952
cwd,
7053
});

0 commit comments

Comments
 (0)