Skip to content

Commit

Permalink
Adding support for 1.0.0 to vscode extension
Browse files Browse the repository at this point in the history
  • Loading branch information
belav committed Jan 2, 2025
1 parent a34fd5a commit 7cb26b1
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 9 deletions.
3 changes: 3 additions & 0 deletions Src/CSharpier.VSCode/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 2.0.0
- Adding support for CSharpier 1.0.0

## 1.9.2
- Add option for diagnostic level, default to warning
- Modify behavior of diagnostics so that new ones only appear on file save, not as a user types.
Expand Down
14 changes: 10 additions & 4 deletions Src/CSharpier.VSCode/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

This extension makes use of the dotnet tool [CSharpier](https://github.com/belav/csharpier) to format your code and is versioned independently.

CSharpier is an opinionated code formatter for c#.
It uses Roslyn to parse your code and re-prints it using its own rules.
CSharpier is an opinionated code formatter for c# and xml.
It provides very few options and provides a deterministic way to enforce formatting of your code.
The printing process was ported from [prettier](https://prettier.io) but has evolved over time.

## Installation
Expand All @@ -27,12 +27,15 @@ The extension makes use of `dotnet` commands and uses the following logic to loc
- For non-windows - Try running `sh -c "dotnet --info"` to see if `dotnet` is on the PATH

## Default Formatter
To ensure that CSharpier is used to format c# files, be sure to set it as the default formatter.
To ensure that CSharpier is used to format files, be sure to set it as the default formatter.

```json
"[csharp]": {
"editor.defaultFormatter": "csharpier.csharpier-vscode"
}
},
"[xml]": {
"editor.defaultFormatter": "csharpier.csharpier-vscode"
},
```

## Usage
Expand All @@ -57,6 +60,9 @@ You can turn on format-on-save on a per-language basis by scoping the setting:
// Enable per-language
"[csharp]": {
"editor.formatOnSave": true
},
"[xml]": {
"editor.formatOnSave": true
}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Logger } from "./Logger";
import { ICSharpierProcess } from "./ICSharpierProcess";
import { getDotNetRoot } from "./DotNetProvider";
import * as process from "process";
import * as semver from "semver";

export class CSharpierProcessPipeMultipleFiles implements ICSharpierProcess {
private process: ChildProcessWithoutNullStreams;
Expand All @@ -29,7 +30,13 @@ export class CSharpierProcessPipeMultipleFiles implements ICSharpierProcess {
}

private spawnProcess = (csharpierPath: string, workingDirectory: string) => {
const csharpierProcess = spawn(csharpierPath, ["--pipe-multiple-files"], {
// TODO test this
let newCommandsVersion = "1.0.0";
let argument = semver.gte(this.version, newCommandsVersion)
? "pipe-files"
: "--pipe-multiple-files";

const csharpierProcess = spawn(csharpierPath, [argument], {
stdio: "pipe",
cwd: workingDirectory,
env: { ...process.env, DOTNET_NOLOGO: "1", DOTNET_ROOT: getDotNetRoot() },
Expand Down
9 changes: 6 additions & 3 deletions Src/CSharpier.VSCode/src/CSharpierProcessServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { Logger } from "./Logger";
import { FormatFileParameter, FormatFileResult, ICSharpierProcess2 } from "./ICSharpierProcess";
import fetch from "node-fetch";
import { getDotNetRoot } from "./DotNetProvider";
import * as semver from "semver";

export class CSharpierProcessServer implements ICSharpierProcess2 {
private csharpierPath: string;
private logger: Logger;
private port: number = 0;
private process: ChildProcessWithoutNullStreams | undefined;
Expand All @@ -14,7 +14,6 @@ export class CSharpierProcessServer implements ICSharpierProcess2 {

constructor(logger: Logger, csharpierPath: string, workingDirectory: string, version: string) {
this.logger = logger;
this.csharpierPath = csharpierPath;
this.spawnProcess(csharpierPath, workingDirectory);
this.version = version;

Expand All @@ -31,7 +30,11 @@ export class CSharpierProcessServer implements ICSharpierProcess2 {
}

private spawnProcess(csharpierPath: string, workingDirectory: string) {
const csharpierProcess = spawn(csharpierPath, ["--server"], {
// TODO test this
let newCommandsVersion = "1.0.0";
let argument = semver.gte(this.version, newCommandsVersion) ? "server" : "--server";

const csharpierProcess = spawn(csharpierPath, [argument], {
stdio: "pipe",
cwd: workingDirectory,
env: { ...process.env, DOTNET_NOLOGO: "1", DOTNET_ROOT: getDotNetRoot() },
Expand Down
5 changes: 4 additions & 1 deletion Src/CSharpier.VSCode/src/CustomPathInstaller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as fs from "fs";
import { workspace } from "vscode";
import { getDotNetRoot, execDotNet } from "./DotNetProvider";
import { execSync } from "child_process";
import * as semver from "semver";

export class CustomPathInstaller {
logger: Logger;
Expand Down Expand Up @@ -92,6 +93,8 @@ export class CustomPathInstaller {
}

public getPathForVersion(version: string) {
return path.resolve(this.getDirectoryForVersion(version), "dotnet-csharpier");
let newCommandsVersion = "1.0.0";
let filename = semver.gte(version, newCommandsVersion) ? "CSharpier" : "dotnet-csharpier";
return path.resolve(this.getDirectoryForVersion(version), filename);
}
}
6 changes: 6 additions & 0 deletions Src/CSharpier.VSCode/src/FormattingService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ import { FormatDocumentProvider } from "./FormatDocumentProvider";

export class FormattingService {
constructor(private readonly formatDocumentProvider: FormatDocumentProvider) {
// TODO will this cover all the actual xml files, maybe we need to use patterns?
// { pattern: '**/*.{props,csproj}' }

languages.registerDocumentFormattingEditProvider("xml", {
provideDocumentFormattingEdits: this.provideDocumentFormattingEdits,
});
languages.registerDocumentFormattingEditProvider("csharp", {
provideDocumentFormattingEdits: this.provideDocumentFormattingEdits,
});
Expand Down

0 comments on commit 7cb26b1

Please sign in to comment.