Skip to content

Commit

Permalink
Improves the partial panel (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
rpaul-stripe authored Jan 9, 2024
1 parent b729cf8 commit b10ee90
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 18 deletions.
16 changes: 13 additions & 3 deletions client/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export default class Extension {
private partials: PartialTreeProvider;
private status: StatusItem;
private preview: Preview;
private active?: Client;

constructor(private context: VSC.ExtensionContext) {
this.partials = new PartialTreeProvider();
Expand Down Expand Up @@ -86,6 +87,9 @@ export default class Extension {
if (clientsToStart.length < 1) return;
this.status.setClient(clientsToStart[0]);
await Promise.allSettled(clientsToStart.map((client) => client.start()));

const uri = VSC.window.activeTextEditor?.document.uri;
if (this.active && uri) this.updatePartialsPane(this.active, uri);
}

async restart() {
Expand Down Expand Up @@ -126,7 +130,9 @@ export default class Extension {
client?.canPreview()
);

if (client) this.status.setClient(client);
if (!client) return;
this.status.setClient(client);
this.active = client;
}

async onActive(editor?: VSC.TextEditor) {
Expand All @@ -140,11 +146,15 @@ export default class Extension {
if (!client) return;
await client.start();

const output = await client.getDependencies(uri);
if (output) this.partials.update(output, client.uri);
this.updatePartialsPane(client, uri);
this.updatePreview(client, uri);
}

async updatePartialsPane(client: Client, uri: VSC.Uri) {
const output = await client.getDependencies(uri);
this.partials.update(output ?? undefined, client.uri);
}

async onNewFileFromTemplate() {
const validStates = [ClientState.Running, ClientState.Initializing];
const options: TemplatePickerItem[] = [];
Expand Down
2 changes: 1 addition & 1 deletion client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "Markdoc Extension",
"author": "Ryan Paul",
"license": "MIT",
"version": "0.0.11",
"version": "0.0.12",
"scripts": {
"build": "esbuild index.ts --bundle --outdir=dist --sourcemap=linked --external:vscode --platform=node --format=cjs",
"build:server": "esbuild server.ts --bundle --outdir=dist --sourcemap=linked --external:vscode --platform=node --format=cjs"
Expand Down
45 changes: 33 additions & 12 deletions client/partials.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,36 @@
import * as VSC from "vscode";
import { DependencyInfo, PartialReference } from "../server/types";

type PartialTreeRoot = { title: string; children?: PartialReference[] };
type PartialTreeItem = PartialTreeRoot | PartialReference;

export default class PartialTreeProvider
implements VSC.TreeDataProvider<PartialReference>
implements VSC.TreeDataProvider<PartialTreeItem>
{
private info?: DependencyInfo;
private uri?: VSC.Uri;

private updateEmitter = new VSC.EventEmitter<void>();
readonly onDidChangeTreeData = this.updateEmitter.event;

update(info: DependencyInfo, uri: VSC.Uri) {
update(info: DependencyInfo | undefined, uri: VSC.Uri) {
this.info = info;
this.uri = uri;
this.updateEmitter.fire();
}

getTreeItem(element: PartialReference): VSC.TreeItem {
getTreeItem(element: PartialTreeItem): VSC.TreeItem {
if (!this.uri) return {};

if ("title" in element) {
const item = new VSC.TreeItem(element.title);
item.collapsibleState =
element?.children && element.children.length > 0
? VSC.TreeItemCollapsibleState.Expanded
: VSC.TreeItemCollapsibleState.None;
return item;
}

const uri = VSC.Uri.joinPath(this.uri, element.file);
const item = new VSC.TreeItem(element.file);
item.command = { command: "vscode.open", title: "Open", arguments: [uri] };
Expand All @@ -31,14 +44,22 @@ export default class PartialTreeProvider
}

getChildren(
element?: PartialReference
): VSC.ProviderResult<PartialReference[]> {
return element
? element.children
: !this.info
? null
: this.info.dependents.length > 0
? this.info.dependents
: this.info.dependencies;
element?: PartialTreeItem
): VSC.ProviderResult<PartialTreeItem[]> {
if (element) return element.children;
if (!this.info) return [{ title: "Loading..." }];

const rootElements = [];
const { dependencies, dependents } = this.info;

if (dependencies.length > 0)
rootElements.push({ title: "Partials", children: dependencies });

if (dependents.length > 0)
rootElements.push({ title: "References", children: dependents });

if (rootElements.length < 1) rootElements.push({ title: "None" });

return rootElements;
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"author": "Ryan Paul",
"publisher": "stripe",
"license": "MIT",
"version": "0.0.11",
"version": "0.0.12",
"description": "A Markdoc language server and Visual Studio Code extension",
"repository": {
"type": "git",
Expand Down
2 changes: 1 addition & 1 deletion server/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@markdoc/language-server",
"version": "0.0.11",
"version": "0.0.12",
"description": "A Markdoc language server",
"main": "dist/index.js",
"author": "Ryan Paul",
Expand Down

0 comments on commit b10ee90

Please sign in to comment.