From b945302c1831b8beb9a5225f97698ff1bf6878fa Mon Sep 17 00:00:00 2001 From: xhayper Date: Sun, 23 Oct 2022 11:15:10 +0700 Subject: [PATCH 1/4] feat: full ipynb-support --- src/activity.ts | 40 +++++++++++++++++++++++++++------------- src/data.ts | 22 ++++++++++++++-------- 2 files changed, 41 insertions(+), 21 deletions(-) diff --git a/src/activity.ts b/src/activity.ts index f0ee69a1..f8d48c4b 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 bb72ba60..da4e94bf 100644 --- a/src/data.ts +++ b/src/data.ts @@ -70,14 +70,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 +100,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 +121,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 +135,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 +279,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) From 15723951ca2b78531b7fbdf4ba9a749355d7bb28 Mon Sep 17 00:00:00 2001 From: xhayper Date: Sun, 23 Oct 2022 11:21:51 +0700 Subject: [PATCH 2/4] feat: mroe ipynb support --- src/activity.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/activity.ts b/src/activity.ts index f8d48c4b..49862d3d 100644 --- a/src/activity.ts +++ b/src/activity.ts @@ -6,7 +6,8 @@ import { env, languages, workspace, - NotebookDocument + NotebookDocument, + NotebookRange } from "vscode"; import { resolveLangName, toLower, toTitle, toUpper } from "./helpers/resolveLangName"; import { type SetActivity } from "@xhayper/discord-rpc"; From 975b838c4e0beb9b0bfc4ccc6096b63fe8c61f07 Mon Sep 17 00:00:00 2001 From: xhayper Date: Sun, 23 Oct 2022 11:24:13 +0700 Subject: [PATCH 3/4] chore: code cleanup --- src/activity.ts | 22 ++++++++++------------ src/data.ts | 1 + 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/src/activity.ts b/src/activity.ts index 49862d3d..853c3f62 100644 --- a/src/activity.ts +++ b/src/activity.ts @@ -1,14 +1,3 @@ -import { - type Selection, - type TextDocument, - debug, - DiagnosticSeverity, - env, - languages, - workspace, - NotebookDocument, - NotebookRange -} from "vscode"; import { resolveLangName, toLower, toTitle, toUpper } from "./helpers/resolveLangName"; import { type SetActivity } from "@xhayper/discord-rpc"; import { CONFIG_KEYS, FAKE_EMPTY } from "./constants"; @@ -18,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; diff --git a/src/data.ts b/src/data.ts index da4e94bf..38c89fdc 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, From 61ec72d2c412efb1ef7f2e973c0606e4f9d724cf Mon Sep 17 00:00:00 2001 From: xhayper Date: Sun, 23 Oct 2022 11:35:13 +0700 Subject: [PATCH 4/4] feat: initial support commit lost to time --- src/controller.ts | 15 ++++++++++++++- src/data.ts | 5 +++++ 2 files changed, 19 insertions(+), 1 deletion(-) 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 38c89fdc..050ff3af 100644 --- a/src/data.ts +++ b/src/data.ts @@ -40,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; @@ -59,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();