Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
nhedger committed Jul 27, 2024
1 parent 047b201 commit 45a7ba1
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 10 deletions.
8 changes: 7 additions & 1 deletion src/commands/start.ts → src/commands.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
import { restart, start, stop } from "../extension";
import { restart, start, stop } from "./extension";

/**
* Starts the Biome extension
*
* This command is exposed to the users as a command in the command palette.
*/
export const startCommand = async () => {
await start();
};

/**
* Stops the Biome extension
*
* This command is exposed to the users as a command in the command palette.
*/
export const stopCommand = async () => {
await stop();
};

/**
* Restarts the Biome extension
*
* This command is exposed to the users as a command in the command palette.
*/
export const restartCommand = async () => {
await restart();
Expand Down
22 changes: 18 additions & 4 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
window,
workspace,
} from "vscode";
import { restartCommand, startCommand, stopCommand } from "./commands/start";
import { restartCommand, startCommand, stopCommand } from "./commands";
import { error, info } from "./logger";
import { type Project, createProjects } from "./project";
import { createSession } from "./session";
Expand Down Expand Up @@ -48,6 +48,8 @@ export const createExtension = async (
return;
}

state.state = "initializing";

// Start the extension
await start();
};
Expand Down Expand Up @@ -79,6 +81,7 @@ const setupGlobalSession = async () => {
if (hasUntitledDocuments() && !state.globalSession) {
info("Found untitle files, creating global session.");
state.globalSession = await createSession();
state.globalSession?.client.start();
}
});

Expand Down Expand Up @@ -108,6 +111,8 @@ const setupProjectSessions = async (projects: Project[]) => {
`Created session for project ${project.path} in workspace ${project.folder.name}.`,
);
state.sessions.set(project, session);

await session.client.start();
} else {
error(
`Failed to create session for project ${project.path} in workspace ${project.folder.name}.`,
Expand All @@ -121,24 +126,32 @@ const setupProjectSessions = async (projects: Project[]) => {
*/
export const start = async () => {
info("🚀 Starting Biome extension");
state.state = "starting";
window.withProgress(
{
title: "Starting Biome extension",
location: ProgressLocation.Notification,
},
async () => {
await setupGlobalSession();
const projects = await createProjects();
await setupProjectSessions(projects);
try {
await setupGlobalSession();
const projects = await createProjects();
await setupProjectSessions(projects);
} catch (e) {
error("Failed to start Biome extension", error);
state.state = "error";
}
},
);
state.state = "started";
};

/**
* Stops the biome extension
*/
export const stop = async () => {
info("Stopping Biome extension");
state.state = "stopping";
window.withProgress(
{
title: "Stopping Biome extension",
Expand All @@ -152,6 +165,7 @@ export const stop = async () => {
state.sessions.clear();
},
);
state.state = "stopped";
};

/**
Expand Down
6 changes: 5 additions & 1 deletion src/session.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { type LogOutputChannel, type Uri, window } from "vscode";
import {
type DocumentFilter,
LanguageClient,
type LanguageClientOptions,
type ServerOptions,
Expand Down Expand Up @@ -60,6 +61,9 @@ const createLanguageClient = (bin: Uri, project?: Project) => {
const serverOptions: ServerOptions = {
command: bin.fsPath,
transport: TransportKind.stdio,
options: {
cwd: project.path.fsPath,
},
args,
};

Expand Down Expand Up @@ -143,7 +147,7 @@ const createLspTraceLogger = (project?: Project): LogOutputChannel => {
* project is specified, the document selector will match files that have
* not yet been saved to disk (untitled).
*/
const createDocumentSelector = (project?: Project) => {
const createDocumentSelector = (project?: Project): DocumentFilter[] => {
return supportedLanguages.map((language) => ({
language,
scheme: project ? "file" : "untitled",
Expand Down
24 changes: 22 additions & 2 deletions src/state.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { workspace } from "vscode";
import type { Project } from "./project";
import type { Session } from "./session";
import { updateStatusBar } from "./ui/status-bar/status-bar";

export type State = {
/**
Expand All @@ -16,7 +18,8 @@ export type State = {
| "started"
| "running"
| "stopping"
| "stopped";
| "stopped"
| "error";

/**
* The Biome project that is currently active
Expand All @@ -32,8 +35,25 @@ export type State = {
globalSession?: Session;
};

export const state: State = {
const _state: State = {
state: "initializing",
sessions: new Map<Project, Session>([]),
globalSession: undefined,
} as State;

/**
* The state of the extension
*
* This state is a proxy to the actual state of the extension, which allows us
* to listen for changes to the state and update the status bar accordingly.
*/
export const state = new Proxy(_state, {
get: (target, prop) => Reflect.get(target, prop),
set: (target, prop, value): boolean => {
if (Reflect.set(target, prop, value)) {
updateStatusBar();
return true;
}
return false;
},
});
17 changes: 15 additions & 2 deletions src/ui/status-bar/status-bar.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import { StatusBarAlignment, type StatusBarItem, window } from "vscode";
import {
StatusBarAlignment,
type StatusBarItem,
window,
workspace,
} from "vscode";
import { type State, state } from "../../state";
import { config } from "../../utils";

export type StatusBar = {
item: StatusBarItem;
};

const createStatusBar = () => {
const createStatusBar = (): StatusBar => {
const item = window.createStatusBarItem(
"biome",
StatusBarAlignment.Right,
Expand All @@ -17,6 +22,10 @@ const createStatusBar = () => {
};

export const updateStatusBar = () => {
if (!state) {
return;
}

if (!config("enable", { default: true }) || state.state === "disabled") {
statusBar.item.hide();
return;
Expand Down Expand Up @@ -60,6 +69,8 @@ const getStateTooltip = () => {
return "Stopping";
case "stopped":
return "Stopped";
case "error":
return "Error";
default:
return "Biome";
}
Expand All @@ -77,6 +88,8 @@ const getStateIcon = (state: State): string => {
return "$(sync~spin)";
case "stopped":
return "$(x)";
case "error":
return "$(error)";
default:
return "$(question)";
}
Expand Down
20 changes: 20 additions & 0 deletions test/fixtures/biome.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* This file exists as a stopgap to ensure that Biome does not attempt
* resolve the configuration files in parent directories for the test
* fixtures that do not have their own configuration files.
*
* This is necessary because some configuration options may have an
* impact on the behavior of the test fixtures, notably, in the root
* of this repo we ignore files in the `test/fixtures` directory to
* ensure we don't format them accidentally. Unfortunately, this
* means that when opening test fixtures that do not have their own
* configuration file as workspace folders, the configuration options
* in the root of the repo will be applied to them, prevent us from
* running the tests reliably.
*
* Test fixtures that do have their own configuration files will be
* resolved as expected.
*
* Keep this configuration empty.
*/
{}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const a = 1;
1 change: 1 addition & 0 deletions test/fixtures/multi-root-workspace/foo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
"version": "0.0.0",
"devDependencies": {
"@biomejs/biome": "^1.8.1"

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const a = 1

0 comments on commit 45a7ba1

Please sign in to comment.