Skip to content

Commit

Permalink
Some cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
kmagiera committed Mar 27, 2024
1 parent 14184bc commit fc72069
Show file tree
Hide file tree
Showing 11 changed files with 254 additions and 184 deletions.
22 changes: 16 additions & 6 deletions packages/vscode-extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,21 @@
"default": null,
"description": "Location of the React Native application root folder relative to the workspace workspace. This is used for monorepo type setups when the workspace root is not the root of the React Native project. The IDE extension tries to locate the React Native application root automatically, but in case it failes to do so (i.e. there are multiple applications defined in the workspace), you can use this setting to override the location."
},
"ReactNativeIDE.showPanelInSideBar":{
"type": "boolean",
"ReactNativeIDE.panelLocation": {
"type": "string",
"scope": "window",
"default": false,
"description": "Moves IDE from tabs to window next to the editor. Toggling this option closes and reopens IDE."
"default": "tab",
"enum": [
"tab",
"side-panel",
"secondary-side-panel"
],
"enumDescriptions": [
"Editor tab",
"Side panel",
"Secondary side panel"
],
"description": "Controlls location of the IDE panel. Changing this option closes and reopens the IDE."
}
}
},
Expand All @@ -75,7 +85,7 @@
"type": "webview",
"id": "ReactNativeIDE.view",
"name": "IDE Panel",
"when": "config.ReactNativeIDE.showPanelInSideBar"
"when": "config.ReactNativeIDE.panelLocation != 'tab'"
}
]
},
Expand All @@ -84,7 +94,7 @@
{
"command": "RNIDE.openPanel",
"group": "navigation",
"when": "RNIDE.extensionIsActive && !RNIDE.panelIsOpen && !config.ReactNativeIDE.showPanelInSideBar"
"when": "RNIDE.extensionIsActive && !RNIDE.panelIsOpen"
}
]
},
Expand Down
1 change: 0 additions & 1 deletion packages/vscode-extension/src/common/Project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ export interface ProjectInterface {
focusBuildOutput(): Promise<void>;
focusExtensionLogsOutput(): Promise<void>;
focusDebugConsole(): Promise<void>;
focusIntoSecondarySidebar(): Promise<void>;
openNavigation(navigationItemID: string): Promise<void>;

dispatchTouch(xRatio: number, yRatio: number, type: "Up" | "Move" | "Down"): Promise<void>;
Expand Down
83 changes: 10 additions & 73 deletions packages/vscode-extension/src/common/WorkspaceConfig.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
import { ConfigurationChangeEvent, workspace, Disposable } from "vscode";
import { Tabpanel } from "../panels/Tabpanel";
import { EventEmitter } from "stream";
import { Logger } from "../Logger";
export type PanelLocation = "tab" | "side-panel" | "secondary-side-panel";

export type WorkspaceConfigProps = {
showPanelInSideBar: boolean;
panelLocation: PanelLocation;
relativeAppLocation: string;
};

export interface WorkspaceConfigEventMap {
workspaceConfigChange: WorkspaceConfigProps;
configChange: WorkspaceConfigProps;
}

export interface WorkspaceConfigEventListener<T> {
(event: T): void;
}

export interface WorkspaceConfigInterface {
getWorkspaceConfigProps(): Promise<WorkspaceConfigProps>;
export interface WorkspaceConfig {
getConfig(): Promise<WorkspaceConfigProps>;
// update method can take any of the keys from WorkspaceConfigProps and appropriate value:
update<K extends keyof WorkspaceConfigProps>(
key: K,
value: WorkspaceConfigProps[K]
): Promise<void>;
addListener<K extends keyof WorkspaceConfigEventMap>(
eventType: K,
listener: WorkspaceConfigEventListener<WorkspaceConfigEventMap[K]>
Expand All @@ -27,68 +29,3 @@ export interface WorkspaceConfigInterface {
listener: WorkspaceConfigEventListener<WorkspaceConfigEventMap[K]>
): Promise<void>;
}

export class WorkspaceConfig implements Disposable, WorkspaceConfigInterface {
public static currentWorkspaceConfig: WorkspaceConfig | undefined;
private workspaceConfigProps: WorkspaceConfigProps;
private eventEmitter = new EventEmitter();
private workspaceConfigListener: Disposable | undefined;

constructor() {
WorkspaceConfig.currentWorkspaceConfig = this;
this.workspaceConfigProps = {
showPanelInSideBar: workspace
.getConfiguration("ReactNativeIDE")
.get<boolean>("showPanelInSideBar")!,
relativeAppLocation: workspace
.getConfiguration("ReactNativeIDE")
.get<string>("relativeAppLocation")!,
};
this.IDEPanelLocationListener();
}

private IDEPanelLocationListener() {
this.workspaceConfigListener = workspace.onDidChangeConfiguration(
(event: ConfigurationChangeEvent) => {
if (!event.affectsConfiguration("ReactNativeIDE")) {
return;
}
if (event.affectsConfiguration("ReactNativeIDE.showPanelInSideBar")) {
this.workspaceConfigProps.showPanelInSideBar = workspace
.getConfiguration("ReactNativeIDE")
.get<boolean>("showPanelInSideBar")!;
if (workspace.getConfiguration("ReactNativeIDE").get("showPanelInSideBar")) {
Tabpanel.currentPanel?.dispose();
}
} else if (event.affectsConfiguration("ReactNativeIDE.relativeAppLocation")) {
this.workspaceConfigProps.relativeAppLocation = workspace
.getConfiguration("ReactNativeIDE")
.get<string>("relativeAppLocation")!;
}
this.eventEmitter.emit("workspaceConfigChange", this.workspaceConfigProps);
}
);
}

async getWorkspaceConfigProps(): Promise<WorkspaceConfigProps> {
return this.workspaceConfigProps;
}

async addListener<K extends keyof WorkspaceConfigEventMap>(
eventType: K,
listener: WorkspaceConfigEventListener<WorkspaceConfigEventMap[K]>
) {
this.eventEmitter.addListener(eventType, listener);
}

async removeListener<K extends keyof WorkspaceConfigEventMap>(
eventType: K,
listener: WorkspaceConfigEventListener<WorkspaceConfigEventMap[K]>
) {
this.eventEmitter.removeListener(eventType, listener);
}

dispose() {
this.workspaceConfigListener?.dispose();
}
}
62 changes: 27 additions & 35 deletions packages/vscode-extension/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import {
Uri,
ExtensionContext,
ExtensionMode,
DebugConfigurationProviderTriggerKind,
ConfigurationChangeEvent,
DebugConfigurationProviderTriggerKind,
} from "vscode";
import { Tabpanel } from "./panels/Tabpanel";
import { TabPanel } from "./panels/Tabpanel";
import { PreviewCodeLensProvider } from "./providers/PreviewCodeLensProvider";
import { DebugConfigProvider } from "./providers/DebugConfigProvider";
import { DebugAdapterDescriptorFactory } from "./debugging/DebugAdapterDescriptorFactory";
Expand All @@ -24,7 +24,8 @@ import { command } from "./utilities/subprocess";
import path from "path";
import os from "os";
import fs from "fs";
import { SidepanelViewProvider } from "./panels/SidepanelViewProvider";
import { SidePanelViewProvider } from "./panels/SidepanelViewProvider";
import { PanelLocation } from "./common/WorkspaceConfig";

const BIN_MODIFICATION_DATE_KEY = "bin_modification_date";
const OPEN_PANEL_ON_ACTIVATION = "open_panel_on_activation";
Expand Down Expand Up @@ -54,38 +55,32 @@ export function deactivate(context: ExtensionContext): undefined {

export async function activate(context: ExtensionContext) {
handleUncaughtErrors();
IDEPanelLocationListener();

setExtensionContext(context);
if (context.extensionMode === ExtensionMode.Development) {
enableDevModeLogging();
}

await fixBinaries(context);

function showIDEPanel(fileName?: string, lineNumber?: number) {
if (
workspace.getConfiguration("ReactNativeIDE").get<PanelLocation>("panelLocation") !== "tab"
) {
SidePanelViewProvider.showView(context, fileName, lineNumber);
} else {
TabPanel.render(context, fileName, lineNumber);
}
}

context.subscriptions.push(
window.registerWebviewViewProvider(
SidepanelViewProvider.viewType,
new SidepanelViewProvider(context)
SidePanelViewProvider.viewType,
new SidePanelViewProvider(context)
)
);
context.subscriptions.push(
commands.registerCommand("RNIDE.openPanel", (fileName?: string, lineNumber?: number) => {
if (workspace.getConfiguration("ReactNativeIDE").get<boolean>("showPanelInSideBar")) {
SidepanelViewProvider.showView(context, fileName, lineNumber);
} else {
Tabpanel.render(context, fileName, lineNumber);
}
})
);
context.subscriptions.push(
commands.registerCommand("RNIDE.showPanel", (fileName?: string, lineNumber?: number) => {
if (workspace.getConfiguration("ReactNativeIDE").get<boolean>("showPanelInSideBar")) {
SidepanelViewProvider.showView(context, fileName, lineNumber);
} else {
Tabpanel.render(context, fileName, lineNumber);
}
})
);
context.subscriptions.push(commands.registerCommand("RNIDE.openPanel", showIDEPanel));
context.subscriptions.push(commands.registerCommand("RNIDE.showPanel", showIDEPanel));
context.subscriptions.push(
commands.registerCommand("RNIDE.diagnose", diagnoseWorkspaceStructure)
);
Expand Down Expand Up @@ -115,18 +110,15 @@ export async function activate(context: ExtensionContext) {
)
);

await configureAppRootFolder();
}
context.subscriptions.push(
workspace.onDidChangeConfiguration((event: ConfigurationChangeEvent) => {
if (event.affectsConfiguration("ReactNativeIDE.panelLocation")) {
showIDEPanel();
}
})
);

function IDEPanelLocationListener() {
workspace.onDidChangeConfiguration((event: ConfigurationChangeEvent) => {
if (!event.affectsConfiguration("ReactNativeIDE")) {
return;
}
if (workspace.getConfiguration("ReactNativeIDE").get("showPanelInSideBar")) {
Tabpanel.currentPanel?.dispose();
}
});
await configureAppRootFolder();
}

async function findSingleFileInWorkspace(fileGlobPattern: string, excludePattern: string | null) {
Expand Down
26 changes: 19 additions & 7 deletions packages/vscode-extension/src/panels/SidepanelViewProvider.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
import { ExtensionContext, Uri, WebviewView, WebviewViewProvider, commands } from "vscode";
import {
ExtensionContext,
Uri,
WebviewView,
WebviewViewProvider,
commands,
workspace,
} from "vscode";
import { generateWebviewContent } from "./webviewContentGenerator";

import { WebviewController } from "./WebviewController";
import { Logger } from "../Logger";

export class SidepanelViewProvider implements WebviewViewProvider {
export class SidePanelViewProvider implements WebviewViewProvider {
public static readonly viewType = "ReactNativeIDE.view";
public static currentProvider: SidepanelViewProvider | undefined;
public static currentProvider: SidePanelViewProvider | undefined;
private _view: any = null;
private webviewController: any = null;

constructor(private readonly context: ExtensionContext) {
SidepanelViewProvider.currentProvider = this;
SidePanelViewProvider.currentProvider = this;
}

refresh(): void {
Expand All @@ -23,15 +30,20 @@ export class SidepanelViewProvider implements WebviewViewProvider {
}

public static showView(context: ExtensionContext, fileName?: string, lineNumber?: number) {
if (SidepanelViewProvider.currentProvider) {
commands.executeCommand(`${SidepanelViewProvider.viewType}.focus`);
if (SidePanelViewProvider.currentProvider) {
commands.executeCommand(`${SidePanelViewProvider.viewType}.focus`);
if (
workspace.getConfiguration("ReactNativeIDE").get("panelLocation") === "secondary-side-panel"
) {
commands.executeCommand("workbench.action.focusAuxiliaryBar");
}
} else {
Logger.error("SidepanelViewProvider does not exist.");
return;
}

if (fileName !== undefined && lineNumber !== undefined) {
SidepanelViewProvider.currentProvider.webviewController.project.startPreview(
SidePanelViewProvider.currentProvider.webviewController.project.startPreview(
`preview:/${fileName}:${lineNumber}`
);
}
Expand Down
35 changes: 26 additions & 9 deletions packages/vscode-extension/src/panels/Tabpanel.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
import { WebviewPanel, window, Uri, ViewColumn, ExtensionContext, commands } from "vscode";
import {
WebviewPanel,
window,
Uri,
ViewColumn,
ExtensionContext,
commands,
workspace,
ConfigurationChangeEvent,
} from "vscode";

import { extensionContext } from "../utilities/extensionContext";
import { generateWebviewContent } from "./webviewContentGenerator";
import { WebviewController } from "./WebviewController";

const OPEN_PANEL_ON_ACTIVATION = "open_panel_on_activation";

export class Tabpanel {
public static currentPanel: Tabpanel | undefined;
export class TabPanel {
public static currentPanel: TabPanel | undefined;
private readonly _panel: WebviewPanel;
private webviewController: WebviewController;

Expand All @@ -26,12 +35,21 @@ export class Tabpanel {
);

this.webviewController = new WebviewController(this._panel.webview);

workspace.onDidChangeConfiguration((event: ConfigurationChangeEvent) => {
if (!event.affectsConfiguration("ReactNativeIDE")) {
return;
}
if (workspace.getConfiguration("ReactNativeIDE").get("panelLocation") !== "tab") {
this.dispose();
}
});
}

public static render(context: ExtensionContext, fileName?: string, lineNumber?: number) {
if (Tabpanel.currentPanel) {
if (TabPanel.currentPanel) {
// If the webview panel already exists reveal it
Tabpanel.currentPanel._panel.reveal(ViewColumn.Beside);
TabPanel.currentPanel._panel.reveal(ViewColumn.Beside);
} else {
// If a webview panel does not already exist create and show a new one

Expand All @@ -51,15 +69,14 @@ export class Tabpanel {
retainContextWhenHidden: true,
}
);
Tabpanel.currentPanel = new Tabpanel(panel);
TabPanel.currentPanel = new TabPanel(panel);
context.workspaceState.update(OPEN_PANEL_ON_ACTIVATION, true);

commands.executeCommand("workbench.action.lockEditorGroup");
commands.executeCommand("setContext", "RNIDE.panelIsOpen", true);
}

if (fileName !== undefined && lineNumber !== undefined) {
Tabpanel.currentPanel.webviewController.project.startPreview(
TabPanel.currentPanel.webviewController.project.startPreview(
`preview:/${fileName}:${lineNumber}`
);
}
Expand All @@ -71,7 +88,7 @@ export class Tabpanel {
// key in this case to prevent extension from automatically opening the panel next time they open the editor
extensionContext.workspaceState.update(OPEN_PANEL_ON_ACTIVATION, undefined);

Tabpanel.currentPanel = undefined;
TabPanel.currentPanel = undefined;

// Dispose of the current webview panel
this._panel.dispose();
Expand Down
Loading

0 comments on commit fc72069

Please sign in to comment.