diff --git a/src/activity.ts b/src/activity.ts index f0ee69a..f8d48c4 100644 --- a/src/activity.ts +++ b/src/activity.ts @@ -1,4 +1,13 @@ -import { type Selection, type TextDocument, debug, DiagnosticSeverity, env, languages, workspace } from "vscode"; +import { + type Selection, + type TextDocument, + debug, + DiagnosticSeverity, + env, + languages, + workspace, + NotebookDocument +} from "vscode"; import { resolveLangName, toLower, toTitle, toUpper } from "./helpers/resolveLangName"; import { type SetActivity } from "@xhayper/discord-rpc"; import { CONFIG_KEYS, FAKE_EMPTY } from "./constants"; @@ -62,7 +71,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 +81,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 +90,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 +111,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 +126,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 +140,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 +150,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 +160,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 +170,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 +186,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 +267,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 +308,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/data.ts b/src/data.ts index 61be151..364754d 100644 --- a/src/data.ts +++ b/src/data.ts @@ -77,14 +77,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; @@ -105,14 +107,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; @@ -124,7 +128,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; @@ -137,7 +142,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); @@ -281,8 +286,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)