Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ipynb support #121

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 27 additions & 14 deletions src/activity.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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;
Expand Down Expand Up @@ -62,15 +70,17 @@ 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;

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
);

Expand All @@ -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);
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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 = [
{
Expand Down Expand Up @@ -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<string> => {
const config = getConfig();
Expand Down Expand Up @@ -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]
]);
Expand Down
15 changes: 14 additions & 1 deletion src/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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) {
Expand Down
28 changes: 20 additions & 8 deletions src/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { logInfo } from "./logger";
import {
type WorkspaceFolder,
type TextEditor,
type NotebookEditor,
type Disposable,
type Extension,
EventEmitter,
Expand Down Expand Up @@ -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;
Expand All @@ -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();
Expand All @@ -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;
Expand All @@ -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;

Expand All @@ -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;
Expand All @@ -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);

Expand Down Expand Up @@ -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)
Expand Down