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

Update VSCode extension to use stdin-filepath and respect-ignores #913

Merged
merged 1 commit into from
Nov 16, 2024
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
5 changes: 5 additions & 0 deletions stylua-vscode/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ To view the changelog of the StyLua binary, see [here](https://github.com/Johnny

## [Unreleased]

### Changed

- The VSCode extension will now defer to using `stylua` itself to determine ignores and other configuration, rather than rolling its own ignore system
- The VSCode extension now passes `--stdin-filepath` and `--respect-ignores` to the command line

## [1.6.3] - 2024-01-06

### Fixed
Expand Down
5 changes: 3 additions & 2 deletions stylua-vscode/package-lock.json

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

1 change: 0 additions & 1 deletion stylua-vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,6 @@
"webpack-cli": "^4.9.2"
},
"dependencies": {
"ignore": "^5.1.8",
"node-fetch": "^2.6.1",
"semver": "^7.5.4",
"unzipper": "^0.10.14",
Expand Down
8 changes: 3 additions & 5 deletions stylua-vscode/src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as vscode from "vscode";
import * as semver from "semver";
import { formatCode, checkIgnored } from "./stylua";
import { formatCode } from "./stylua";
import { GitHub, GitHubRelease } from "./github";
import { ResolveMode, StyluaDownloader, StyluaInfo } from "./download";
import { getDesiredVersion } from "./util";
Expand Down Expand Up @@ -241,16 +241,14 @@ export async function activate(context: vscode.ExtensionContext) {
);
const cwd = currentWorkspace?.uri?.fsPath;

if (await checkIgnored(document.uri, currentWorkspace?.uri)) {
return [];
}

const text = document.getText();

try {
const formattedText = await formatCode(
outputChannel,
styluaBinaryPath.path,
text,
document.uri.fsPath,
cwd,
byteOffset(document, range.start),
byteOffset(document, range.end)
Expand Down
39 changes: 11 additions & 28 deletions stylua-vscode/src/stylua.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,23 @@
import * as vscode from "vscode";
import { spawn, exec } from "child_process";
import ignore from "ignore";
import { fileExists } from "./util";

export async function checkIgnored(
filePath?: vscode.Uri,
currentWorkspace?: vscode.Uri
): Promise<boolean> {
if (!filePath || !currentWorkspace) {
return false;
}

const ignoreFilePath = vscode.Uri.joinPath(currentWorkspace, ".styluaignore");
if (await fileExists(ignoreFilePath)) {
try {
const contents = await vscode.workspace.fs.readFile(ignoreFilePath);
const ig = ignore().add(contents.toString());
return ig.ignores(vscode.workspace.asRelativePath(filePath));
} catch (err) {
vscode.window.showErrorMessage(
`Could not read StyLua ignore file at ${ignoreFilePath}:\n${err}`
);
return false;
}
}

return false;
}

export function formatCode(
outputChannel: vscode.LogOutputChannel,
path: string,
code: string,
filePath?: string,
cwd?: string,
startPos?: number,
endPos?: number
): Promise<string> {
return new Promise((resolve, reject) => {
const args = [];
const args = ["--respect-ignores"];

if (filePath) {
args.push("--stdin-filepath");
args.push(filePath);
}

if (startPos) {
args.push("--range-start");
args.push(startPos.toString());
Expand Down Expand Up @@ -65,6 +46,8 @@ export function formatCode(

args.push("-");

outputChannel.debug(`${path} {args.join(" ")}`);

const child = spawn(`${path}`, args, {
cwd,
});
Expand Down
Loading