Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
the basics of #1109
  • Loading branch information
belav committed Apr 12, 2024
1 parent ea02607 commit 38e1dae
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 15 deletions.
11 changes: 9 additions & 2 deletions Src/CSharpier.VSCode/src/CSharpierProcessPipeMultipleFiles.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
import { ChildProcessWithoutNullStreams, spawn } from "child_process";
import { Logger } from "./Logger";
import { ICSharpierProcess } from "./CSharpierProcess";
import { ICSharpierProcess } from "./ICSharpierProcess";
import { getDotNetRoot } from "./DotNetProvider";
import * as process from "process";

export class CSharpierProcessPipeMultipleFiles implements ICSharpierProcess {
private process: ChildProcessWithoutNullStreams;
private callbacks: ((result: string) => void)[] = [];
private logger: Logger;
private nextFile: string = "";
public processFailedToStart = false;
private version: string;

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

this.logger.debug("Warm CSharpier with initial format");
// warm by formatting a file twice, the 3rd time is when it gets really fast
Expand Down Expand Up @@ -96,4 +99,8 @@ export class CSharpierProcessPipeMultipleFiles implements ICSharpierProcess {
(this.process.stdin as any).pause();
this.process.kill();
}

getVersion(): string {
return this.version;
}
}
11 changes: 8 additions & 3 deletions Src/CSharpier.VSCode/src/CSharpierProcessProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ import { Logger } from "./Logger";
import * as path from "path";
import * as semver from "semver";
import * as convert from "xml-js";
import { ICSharpierProcess, NullCSharpierProcess } from "./CSharpierProcess";
import { ICSharpierProcess } from "./CSharpierProcess";
import { CSharpierProcessSingleFile } from "./CSharpierProcessSingleFile";
import { CSharpierProcessPipeMultipleFiles } from "./CSharpierProcessPipeMultipleFiles";
import * as fs from "fs";
import { InstallerService } from "./InstallerService";
import { CustomPathInstaller } from "./CustomPathInstaller";
import { execDotNet } from "./DotNetProvider";
import {NullCSharpierProcess} from "./NullCSharpierProcess";
import {CSharpierProcessServer} from "./CSharpierProcessServer";

export class CSharpierProcessProvider implements Disposable {
warnedForOldVersion = false;
Expand Down Expand Up @@ -218,19 +220,22 @@ export class CSharpierProcessProvider implements Disposable {

this.logger.debug(`Adding new version ${version} process for ${directory}`);

if (semver.lt(version, "0.12.0")) {
if (semver.gte(version, "0.28.0")) {
return new CSharpierProcessServer(this.logger, customPath, directory, version);
} else if (semver.lt(version, "0.12.0")) {
if (!this.warnedForOldVersion) {
window.showInformationMessage(
"Please upgrade to CSharpier >= 0.12.0 for bug fixes and improved formatting speed.",
);
this.warnedForOldVersion = true;
}
return new CSharpierProcessSingleFile(this.logger, customPath);
return new CSharpierProcessSingleFile(this.logger, customPath, version);
} else {
const csharpierProcess = new CSharpierProcessPipeMultipleFiles(
this.logger,
customPath,
directory,
version
);
if (csharpierProcess.processFailedToStart) {
this.displayFailureMessage();
Expand Down
41 changes: 41 additions & 0 deletions Src/CSharpier.VSCode/src/CSharpierProcessServer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { ChildProcessWithoutNullStreams, spawn } from "child_process";
import { Logger } from "./Logger";
import {FormatFileParameter, FormatFileResult, ICSharpierProcess2} from "./ICSharpierProcess";
import { getDotNetRoot } from "./DotNetProvider";

// TODO lots of stuff in here
// https://github.com/belav/csharpier/pull/1127/files
// https://github.com/belav/csharpier/pull/1203/files

export class CSharpierProcessServer implements ICSharpierProcess2 {
private process: ChildProcessWithoutNullStreams;
private logger: Logger;
private version: string;
constructor(logger: Logger, csharpierPath: string, workingDirectory: string, version: string) {
this.logger = logger;
this.process = this.spawnProcess(csharpierPath, workingDirectory);
this.version = version;

this.logger.debug("Warm CSharpier with initial format");
// warm by formatting a file twice, the 3rd time is when it gets really fast
this.formatFile("public class ClassName { }", "Test.cs").then(() => {
this.formatFile("public class ClassName { }", "Test.cs");
});
}

dispose(): any {
}

formatFile(content: string, filePath: string): Promise<string> {
return Promise.resolve("");
}

formatFile2(parameter: FormatFileParameter): FormatFileResult {
return {} as any;
}

getVersion(): string {
return this.version;
}

}
11 changes: 9 additions & 2 deletions Src/CSharpier.VSCode/src/CSharpierProcessSingleFile.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import { Logger } from "./Logger";
import { spawn } from "child_process";
import { ICSharpierProcess } from "./CSharpierProcess";
import { ICSharpierProcess } from "./ICSharpierProcess";
import * as path from "path";
import { getDotNetRoot } from "./DotNetProvider";
import * as process from "process";

export class CSharpierProcessSingleFile implements ICSharpierProcess {
private readonly csharpierPath: string;
private logger: Logger;
private version: string;

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

formatFile(content: string, filePath: string): Promise<string> {
Expand Down Expand Up @@ -40,4 +43,8 @@ export class CSharpierProcessSingleFile implements ICSharpierProcess {
}

dispose() {}

getVersion(): string {
return this.version;
}
}
2 changes: 1 addition & 1 deletion Src/CSharpier.VSCode/src/Extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { ExtensionContext, window, workspace } from "vscode";
import { CSharpierProcessProvider } from "./CSharpierProcessProvider";
import { FormattingService } from "./FormattingService";
import { Logger } from "./Logger";
import { NullCSharpierProcess } from "./CSharpierProcess";
import { findDotNet } from "./DotNetProvider";
import { options } from "./Options";
import {NullCSharpierProcess} from "./NullCSharpierProcess";

export async function activate(context: ExtensionContext) {
if (!workspace.isTrusted) {
Expand Down
1 change: 1 addition & 0 deletions Src/CSharpier.VSCode/src/FormattingService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export class FormattingService {
});
}

// TODO lots of changes in here for which interface we use to format
private provideDocumentFormattingEdits = async (document: TextDocument) => {
this.logger.info("Formatting started for " + document.fileName + ".");
const startTime = performance.now();
Expand Down
29 changes: 29 additions & 0 deletions Src/CSharpier.VSCode/src/ICSharpierProcess.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import {Disposable} from "vscode";

export interface ICSharpierProcess extends Disposable {
getVersion(): string;
formatFile(content: string, filePath: string): Promise<string>;
}

export interface ICSharpierProcess2 extends ICSharpierProcess {
formatFile2(parameter: FormatFileParameter): FormatFileResult;
}

export interface FormatFileParameter {
fileContents: string;
fileName: string;
}

export interface FormatFileResult {
formattedFile: string;
status: Status;
errorMessage: string;
}

export enum Status
{
Formatted,
Ignored,
Failed
}

Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import { Disposable } from "vscode";
import { Logger } from "./Logger";

export interface ICSharpierProcess extends Disposable {
formatFile(content: string, filePath: string): Promise<string>;
}
import {Logger} from "./Logger";
import {ICSharpierProcess} from "./CSharpierProcess";

export class NullCSharpierProcess implements ICSharpierProcess {
static instance: NullCSharpierProcess;
Expand All @@ -22,5 +18,10 @@ export class NullCSharpierProcess implements ICSharpierProcess {
return Promise.resolve("");
}

dispose() {}
dispose() {
}

getVersion(): string {
return "NULL";
}
}

0 comments on commit 38e1dae

Please sign in to comment.