diff --git a/src/activity.ts b/src/activity.ts index f0ee69a1..853c3f62 100644 --- a/src/activity.ts +++ b/src/activity.ts @@ -1,4 +1,3 @@ -import { type Selection, type TextDocument, debug, DiagnosticSeverity, env, languages, workspace } from "vscode"; import { resolveLangName, toLower, toTitle, toUpper } from "./helpers/resolveLangName"; import { type SetActivity } from "@xhayper/discord-rpc"; import { CONFIG_KEYS, FAKE_EMPTY } from "./constants"; @@ -8,7 +7,16 @@ import { isObject } from "./helpers/isObject"; import { getConfig } from "./config"; import { dataClass } from "./data"; import { sep } from "node:path"; -import { logInfo } from "./logger"; +import { + type Selection, + type TextDocument, + type NotebookDocument, + debug, + DiagnosticSeverity, + env, + languages, + workspace +} from "vscode"; // TODO: move this to data class export let totalProblems = 0; @@ -62,7 +70,9 @@ export const activity = async ( (isExcluded(config.get(CONFIG_KEYS.Ignore.Workspaces), dataClass.workspaceFolder.uri.fsPath) || isExcluded(config.get(CONFIG_KEYS.Ignore.Workspaces), dataClass.workspaceName)); - isIdling = isIdling || (!isWorkspaceExcluded && (!dataClass.workspaceFolder || !dataClass.editor)); + isIdling = + isIdling || + (!isWorkspaceExcluded && (!dataClass.workspaceFolder || (!dataClass.editor && !dataClass.notebookEditor))); const isDebugging = !!debug.activeDebugSession; isViewing = !isDebugging && isViewing; @@ -70,7 +80,7 @@ export const activity = async ( const PROBLEMS = await replaceFileInfo( replaceGitInfo(replaceAppInfo(config.get(CONFIG_KEYS.Status.Problems.Text)), isGitExcluded), isWorkspaceExcluded, - dataClass.editor?.document, + dataClass.editor?.document ?? dataClass.notebookEditor?.notebook, dataClass.editor?.selection ); @@ -79,7 +89,7 @@ export const activity = async ( await replaceFileInfo( replaceGitInfo(replaceAppInfo(text), isGitExcluded), isWorkspaceExcluded, - dataClass.editor?.document, + dataClass.editor?.document ?? dataClass.notebookEditor?.notebook, dataClass.editor?.selection ) ).replaceAll("{problems}", PROBLEMS); @@ -100,7 +110,7 @@ export const activity = async ( const detailsText = detailsEnabled ? isWorkspaceExcluded ? workspaceExcludedText - : isIdling || !dataClass.editor + : isIdling || (!dataClass.editor && !dataClass.notebookEditor) ? detailsIdleEnabled ? await replaceAllText(config.get(CONFIG_KEYS.Status.Details.Text.Idle)) : undefined @@ -115,7 +125,7 @@ export const activity = async ( const stateText = stateEnabled && !isWorkspaceExcluded - ? isIdling || !dataClass.editor + ? isIdling || (!dataClass.editor && !dataClass.notebookEditor) ? stateIdleEnabled ? await replaceAllText(config.get(CONFIG_KEYS.Status.State.Text.Idle)) : undefined @@ -129,7 +139,7 @@ export const activity = async ( : undefined; const largeImageKey = await replaceAllText( - isIdling || !dataClass.editor + isIdling || (!dataClass.editor && !dataClass.notebookEditor) ? config.get(CONFIG_KEYS.Status.Image.Large.Idle.Key) : isDebugging ? config.get(CONFIG_KEYS.Status.Image.Large.Debugging.Key) @@ -139,7 +149,7 @@ export const activity = async ( ); const largeImageText = await replaceAllText( - isIdling || !dataClass.editor + isIdling || (!dataClass.editor && !dataClass.notebookEditor) ? config.get(CONFIG_KEYS.Status.Image.Large.Idle.Text) : isDebugging ? config.get(CONFIG_KEYS.Status.Image.Large.Debugging.Text) @@ -149,7 +159,7 @@ export const activity = async ( ); const smallImageKey = await replaceAllText( - isIdling || !dataClass.editor + isIdling || (!dataClass.editor && !dataClass.notebookEditor) ? config.get(CONFIG_KEYS.Status.Image.Small.Idle.Key) : isDebugging ? config.get(CONFIG_KEYS.Status.Image.Small.Debugging.Key) @@ -159,7 +169,7 @@ export const activity = async ( ); const smallImageText = await replaceAllText( - isIdling || !dataClass.editor + isIdling || (!dataClass.editor && !dataClass.notebookEditor) ? config.get(CONFIG_KEYS.Status.Image.Small.Idle.Text) : isDebugging ? config.get(CONFIG_KEYS.Status.Image.Small.Debugging.Text) @@ -175,7 +185,7 @@ export const activity = async ( presence.smallImageKey = smallImageKey; presence.smallImageText = smallImageText; - if (isIdling || !dataClass.editor) { + if (isIdling || (!dataClass.editor && !dataClass.notebookEditor)) { if (config.get(CONFIG_KEYS.Status.Button.Idle.Enabled)) presence.buttons = [ { @@ -256,7 +266,7 @@ export const replaceGitInfo = (text: string, excluded: boolean = false): string export const replaceFileInfo = async ( text: string, excluded: boolean = false, - document?: TextDocument, + document?: TextDocument | NotebookDocument, selection?: Selection ): Promise => { const config = getConfig(); @@ -297,7 +307,10 @@ export const replaceFileInfo = async ( "{problems_count}", config.get(CONFIG_KEYS.Status.Problems.Enabled) ? totalProblems.toLocaleString() : FAKE_EMPTY ], - ["{line_count}", document?.lineCount.toLocaleString() ?? FAKE_EMPTY], + [ + "{line_count}", + (document && "lineCount" in document ? document.lineCount.toLocaleString() : undefined) ?? FAKE_EMPTY + ], ["{current_line}", selection ? (selection.active.line + 1).toLocaleString() : FAKE_EMPTY], ["{current_column}", selection ? (selection.active.character + 1).toLocaleString() : FAKE_EMPTY] ]); diff --git a/src/controller.ts b/src/controller.ts index f29ba24c..4a15144c 100644 --- a/src/controller.ts +++ b/src/controller.ts @@ -80,8 +80,11 @@ export class RPCController { }; const fileSwitch = window.onDidChangeActiveTextEditor(() => sendActivity(true)); + const notebookSwitch = window.onDidChangeActiveNotebookEditor(() => sendActivity(true)); const fileEdit = workspace.onDidChangeTextDocument(this.activityThrottle.callable); + const notebookEdit = workspace.onDidChangeNotebookDocument(this.activityThrottle.callable); const fileSelectionChanged = window.onDidChangeTextEditorSelection(this.activityThrottle.callable); + const notebookSelectionChanged = window.onDidChangeNotebookEditorSelection(this.activityThrottle.callable); const debugStart = debug.onDidStartDebugSession(() => sendActivity()); const debugEnd = debug.onDidTerminateDebugSession(() => sendActivity()); const diagnosticsChange = languages.onDidChangeDiagnostics(() => onDiagnosticsChange()); @@ -91,7 +94,17 @@ export class RPCController { if (config.get(CONFIG_KEYS.Status.Problems.Enabled)) this.listeners.push(diagnosticsChange); if (config.get(CONFIG_KEYS.Status.Idle.Check)) this.listeners.push(changeWindowState); - this.listeners.push(fileSwitch, fileEdit, fileSelectionChanged, debugStart, debugEnd, gitListener); + this.listeners.push( + fileSwitch, + notebookSwitch, + fileEdit, + notebookEdit, + fileSelectionChanged, + notebookSelectionChanged, + debugStart, + debugEnd, + gitListener + ); } private async checkIdle(windowState: WindowState) { diff --git a/src/data.ts b/src/data.ts index bb72ba60..050ff3af 100644 --- a/src/data.ts +++ b/src/data.ts @@ -7,6 +7,7 @@ import { logInfo } from "./logger"; import { type WorkspaceFolder, type TextEditor, + type NotebookEditor, type Disposable, type Extension, EventEmitter, @@ -39,6 +40,7 @@ export class Data implements DisposableLike { private gitApi: GitApi | undefined; public editor: TextEditor | undefined; + public notebookEditor: NotebookEditor | undefined; public constructor(debug: boolean = false) { this._debug = debug; @@ -58,6 +60,10 @@ export class Data implements DisposableLike { this.editor = e; this.updateGit(); }), + window.onDidChangeActiveNotebookEditor((e) => { + this.notebookEditor = e; + this.updateGit(); + }), workspace.onDidChangeWorkspaceFolders(() => { this.debug("root(): workspace.onDidChangeWorkspaceFolders"); this.updateGit(); @@ -70,14 +76,16 @@ export class Data implements DisposableLike { } public get fileName(): string | undefined { - const _file = this.editor ? parse(this.editor.document.uri.fsPath) : undefined; + const uri = this.editor?.document.uri ?? this.notebookEditor?.notebook.uri; + const _file = uri ? parse(uri.fsPath) : undefined; const v = _file ? _file.name : undefined; this.debug(`fileName(): ${v}`); return v; } public get fileExtension(): string | undefined { - const _file = this.editor ? parse(this.editor.document.uri.fsPath) : undefined; + const uri = this.editor?.document.uri ?? this.notebookEditor?.notebook.uri; + const _file = uri ? parse(uri.fsPath) : undefined; const v = _file ? _file.ext : undefined; this.debug(`fileExtension(): ${v}`); return v; @@ -98,14 +106,16 @@ export class Data implements DisposableLike { } public get dirName(): string | undefined { - const _file = this.editor ? parse(this.editor.document.uri.fsPath) : undefined; + const uri = this.editor?.document.uri ?? this.notebookEditor?.notebook.uri; + const _file = uri ? parse(uri.fsPath) : undefined; const v = basename(_file?.dir ?? ""); this.debug(`dirName(): ${v}`); return v; } public get folderAndFile(): string | undefined { - const _file = this.editor ? parse(this.editor.document.uri.fsPath) : undefined; + const uri = this.editor?.document.uri ?? this.notebookEditor?.notebook.uri; + const _file = uri ? parse(uri.fsPath) : undefined; const directory = basename(_file?.dir ?? ""); const file = _file ? _file.base : undefined; @@ -117,7 +127,8 @@ export class Data implements DisposableLike { } public get fullDirName(): string | undefined { - const _file = this.editor ? parse(this.editor.document.uri.fsPath) : undefined; + const uri = this.editor?.document.uri ?? this.notebookEditor?.notebook.uri; + const _file = uri ? parse(uri.fsPath) : undefined; const v = _file?.dir; this.debug(`fullDirName(): ${v}`); return v; @@ -130,7 +141,7 @@ export class Data implements DisposableLike { } public get workspaceFolder(): WorkspaceFolder | undefined { - const uri = this.editor?.document.uri; + const uri = this.editor?.document.uri ?? this.notebookEditor?.notebook.uri; let v: WorkspaceFolder | undefined; if (uri) v = workspace.getWorkspaceFolder(uri); @@ -274,8 +285,9 @@ export class Data implements DisposableLike { const repos = this.gitApi.repositories; - if (this.editor) { - const _file = parse(this.editor.document.uri.fsPath); + if (this.editor || this.notebookEditor) { + const uri = this.editor?.document.uri ?? this.notebookEditor?.notebook.uri; + const _file = parse(uri!.fsPath); const testString = _file.dir; return repos .filter((v) => v.rootUri.fsPath.length <= testString.length)