Skip to content

Commit

Permalink
feat: full ipynb-support
Browse files Browse the repository at this point in the history
  • Loading branch information
xhayper committed Oct 23, 2022
1 parent 2332c51 commit 3504a7c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 21 deletions.
40 changes: 27 additions & 13 deletions src/activity.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -62,15 +71,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 +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);
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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 = [
{
Expand Down Expand Up @@ -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<string> => {
const config = getConfig();
Expand Down Expand Up @@ -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]
]);
Expand Down
22 changes: 14 additions & 8 deletions src/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

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

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

0 comments on commit 3504a7c

Please sign in to comment.