Skip to content

Commit

Permalink
Renaming
Browse files Browse the repository at this point in the history
  • Loading branch information
flengyel committed Jul 6, 2023
1 parent e3a83f3 commit 98de52c
Show file tree
Hide file tree
Showing 13 changed files with 193 additions and 157 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,11 @@
},
{
"command": "zettelView.renameEntry",
"title": "Rename and replace links"
"title": "Rename and replace IDs"
},
{
"command": "incomingZettelView.refreshEntry",
"title": "Refresh Incoming Links",
"title": "Refresh Incoming IDs",
"icon": {
"light": "resources/light/refresh.svg",
"dark": "resources/dark/refresh.svg"
Expand Down
27 changes: 17 additions & 10 deletions src/AsyncZettelViewTreeItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,27 @@ import * as vscode from 'vscode';
import * as fs from 'fs';
import * as readline from 'readline';
import { idRegex, extractIDFromFilename } from './utils/utils';
import { IncomingLinksMap } from './utils/IncomingLinksMap';
import { IncomingIDMap } from './utils/IncomingIDMap';
import { MyLogger } from './utils/MyLogger';
//import { IncomingLinksViewTreeDataProvider } from './IncomingLinksViewTreeDataProvider';

export class AsyncZettelViewTreeItem extends vscode.TreeItem {
// public label: string;
public isReady: Promise<AsyncZettelViewTreeItem>;
public incomingLinks: Set<string> = new Set<string>();
public incomingIDs: Set<string> = new Set<string>();
private idMatch = false;
private markdownID: string;

constructor(
public readonly pathname: string,
public readonly basename: string,
public readonly collapsibleState: vscode.TreeItemCollapsibleState,
private incomingLinksMap: IncomingLinksMap,
private incomingIDMap: IncomingIDMap,
public readonly command?: vscode.Command,
) {
super(basename, collapsibleState);
this.label = basename;

MyLogger.logMsg(`AsyncZettelViewTreeItem constructor: this.label is ${this.label}`);
// MyLogger.logMsg(`AsyncZettelViewTreeItem constructor: this.label is ${this.label}`);
this.markdownID = extractIDFromFilename(basename);

// since we cannot make an asynchronous call to a constructor
Expand Down Expand Up @@ -57,15 +56,18 @@ export class AsyncZettelViewTreeItem extends vscode.TreeItem {
} else {
this.idMatch = true;
this.label = line;
MyLogger.logMsg(`ID MATCH: this.label is supposed to be ${this.label}`);
//MyLogger.logMsg(`ID MATCH: this.label is supposed to be ${this.label}`);
}
}
// all links are of the form [[ID]] or [[ID|TITLE]]
// all IDs are of the form [[ID]] or [[ID|TITLE]]
// I don't know if Zettlr understands [[ID|TITLE]]
let match;
while ((match = idRegex.linkRegExp.exec(line)) !== null) {
this.incomingLinksMap.addLink(match[1], this.markdownID);
//MyLogger.logMsg(`Added incoming link from ${match[1]} to ${this.markdownID}`);
this.incomingIDMap.addID(this.markdownID, match[1]);
if (this.markdownID == `ham.2.0.23.0124.1142`) {
// what is going on with this ID?
MyLogger.logMsg(`Added incoming link from ${this.markdownID} to ${match[1]}`);
}
}
}

Expand All @@ -74,10 +76,15 @@ export class AsyncZettelViewTreeItem extends vscode.TreeItem {
}

rl.close();
this.incomingLinks = this.incomingLinksMap.getIncomingLinksFor(this.markdownID);
this.incomingIDs = this.incomingIDMap.getIncomingIDsFor(this.markdownID);
if (this.markdownID == `ham.2.0.23.0124.1142`) {
MyLogger.logMsg(`Incoming IDs for ${this.markdownID}: ${Array.from(this.incomingIDs)}`);
}
//MyLogger.logMsg(`Incoming IDs for ${this.markdownID}: ${Array.from(this.incomingIDs)}`);

return this;
} catch (err) {
this.label = basename; // a default value
console.error(err);
return this;
}
Expand Down
63 changes: 0 additions & 63 deletions src/IncomingLinksViewTreeDataProvider.ts

This file was deleted.

96 changes: 96 additions & 0 deletions src/IncomingZettelViewTreeDataProvider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import * as vscode from 'vscode';
import * as path from 'path';
import { AsyncZettelViewTreeItem } from './AsyncZettelViewTreeItem';
import { IncomingIDMap } from './utils/IncomingIDMap';
import { MyLogger } from './utils/MyLogger';

export class IncomingZettelViewTreeDataProvider implements vscode.TreeDataProvider<AsyncZettelViewTreeItem> {
private _onDidChangeTreeData: vscode.EventEmitter<undefined> = new vscode.EventEmitter<undefined>();
readonly onDidChangeTreeData: vscode.Event<undefined> = this._onDidChangeTreeData.event;

private currentMarkdownFile: string | null = null;

constructor(private workspaceRoot: string, private incomingIDMap: IncomingIDMap) {
vscode.workspace.onDidOpenTextDocument((document) => {
if (document.languageId === 'markdown') {
this.currentMarkdownFile = document.fileName;
this.refresh();
}
});
vscode.window.onDidChangeActiveTextEditor(editor => {
if (editor) {
MyLogger.logMsg(`Editor changed: ${editor.document.fileName}`);
if (editor.document.languageId === 'markdown') {
this.currentMarkdownFile = editor.document.fileName;
MyLogger.logMsg(`Current markdown file is ${this.currentMarkdownFile}`);
this.refresh();
} else {
MyLogger.logMsg(`Editor is not a markdown file.`);
}
} else {
MyLogger.logMsg(`No active editor.`);
}
});
}


//constructor(private workspaceRoot: string, private incomingLinksMap: IncomingLinksMap) {
//vscode.window.onDidChangeActiveTextEditor(editor => {
// if (editor && editor.document.languageId === 'markdown') {
// this.currentMarkdownFile = editor.document.fileName;
//MyLogger.logMsg(`Incoming Zettel View: Current markdown file is ${this.currentMarkdownFile}`);
// this.refresh();
// }
//});
//}

refresh(): void {
this._onDidChangeTreeData.fire(undefined);
}

getTreeItem(element: AsyncZettelViewTreeItem): vscode.TreeItem {
return element;
}

async getChildren(element?: AsyncZettelViewTreeItem): Promise<AsyncZettelViewTreeItem[]> {
if (!this.currentMarkdownFile) {
vscode.window.showInformationMessage('Incoming Zettel View: No markdown file in focus');
MyLogger.logMsg('Incoming Zettel View: No markdown file in focus');
return [];
}

const id = path.basename(this.currentMarkdownFile, '.md');
const incomingIDs = this.incomingIDMap.getIncomingIDsFor(id);
MyLogger.logMsg(`Incoming IDs for ${id}: ${JSON.stringify(incomingIDs)}`); // Add this log

if (!incomingIDs || incomingIDs.size === 0) {
vscode.window.showInformationMessage(`No incoming IDs for the file ${this.currentMarkdownFile}`);
MyLogger.logMsg(`No incoming IDs for the file ${this.currentMarkdownFile}`);
return [];
}


const items = await Promise.all(Array.from(incomingIDs).map(async (id: string) => {
const pathname = path.join(this.workspaceRoot, `${id}.md`);
const basename = `${id}.md`;
const item = new AsyncZettelViewTreeItem(
pathname,
basename,
vscode.TreeItemCollapsibleState.None,
this.incomingIDMap,
{
command: 'vscode.open',
title: '',
arguments: [vscode.Uri.file(pathname)],
}
);

await item.isReady;
return item;
}));

return items;
}


}
6 changes: 3 additions & 3 deletions src/ZettelViewTreeDataProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as vscode from 'vscode';
import * as fs from 'fs';
import * as path from 'path';
import { AsyncZettelViewTreeItem } from './AsyncZettelViewTreeItem';
import { IncomingLinksMap } from './utils/IncomingLinksMap';
import { IncomingIDMap } from './utils/IncomingIDMap';
import { MyLogger } from './utils/MyLogger';

export class ZettelViewTreeDataProvider implements vscode.TreeDataProvider<AsyncZettelViewTreeItem> {
Expand All @@ -15,7 +15,7 @@ export class ZettelViewTreeDataProvider implements vscode.TreeDataProvider<Async
// Trigger this event whenever a Zettel is selected in the main view


constructor(private workspaceRoot: string, private incomingLinksMap: IncomingLinksMap) {}
constructor(private workspaceRoot: string, private incomingIDMap: IncomingIDMap) {}

onZettelSelected(zettel: AsyncZettelViewTreeItem): void {
zettel.isReady.then(() => {
Expand Down Expand Up @@ -50,7 +50,7 @@ export class ZettelViewTreeDataProvider implements vscode.TreeDataProvider<Async
path.join(this.workspaceRoot, file),
file,
vscode.TreeItemCollapsibleState.None,
this.incomingLinksMap,
this.incomingIDMap,
{
command: 'zettelkasten.openZettel',
title: '',
Expand Down
17 changes: 8 additions & 9 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@ import * as fs from 'fs';
import * as path from 'path';
import { MyLogger } from './utils/MyLogger';
import { idRegex } from './utils/utils'; // Add the import statement for replaceIncomingLinks
import { replaceIncomingLinks } from './utils/replaceIncomingLinks'; // Add the import statement for replaceIncomingLinks
import { replaceIncomingIDs } from './utils/replaceIncomingIDs'; // Add the import statement for replaceIncomingLinks
import { AsyncZettelViewTreeItem } from './AsyncZettelViewTreeItem';
import { ZettelViewTreeDataProvider } from './ZettelViewTreeDataProvider';
import { IncomingLinksMap } from './utils/IncomingLinksMap';
import { IncomingLinksViewTreeDataProvider } from './IncomingLinksViewTreeDataProvider';
import { IncomingIDMap, incomingIDMap } from './utils/IncomingIDMap';
import { IncomingZettelViewTreeDataProvider } from './IncomingZettelViewTreeDataProvider';


const incomingLinksMap = new IncomingLinksMap();

async function selectZettel(zettel: AsyncZettelViewTreeItem,
zvtdProvider: ZettelViewTreeDataProvider) {
Expand All @@ -26,13 +25,13 @@ export function activate(context: vscode.ExtensionContext): void {
? vscode.workspace.workspaceFolders[0].uri.fsPath : undefined;

if (workspaceRoot) {
const zettelViewTreeDataProvider = new ZettelViewTreeDataProvider(workspaceRoot, incomingLinksMap);
const zettelViewTreeDataProvider = new ZettelViewTreeDataProvider(workspaceRoot, incomingIDMap);
vscode.window.registerTreeDataProvider('zettelView', zettelViewTreeDataProvider);

vscode.commands.registerCommand('zettelView.refreshEntry', () => zettelViewTreeDataProvider.refresh());
registerRenameCommand(zettelViewTreeDataProvider, workspaceRoot, incomingLinksMap); // Pass the workspaceRoot and incomingLinksMap to the function
registerRenameCommand(zettelViewTreeDataProvider, workspaceRoot, incomingIDMap); // Pass the workspaceRoot and incomingLinksMap to the function

const incomingZettelViewProvider = new IncomingLinksViewTreeDataProvider(workspaceRoot, incomingLinksMap);
const incomingZettelViewProvider = new IncomingZettelViewTreeDataProvider(workspaceRoot, incomingIDMap);
vscode.window.registerTreeDataProvider('incomingZettelView', incomingZettelViewProvider);

vscode.commands.registerCommand('incomingZettelView.refreshEntry', () => incomingZettelViewProvider.refresh());
Expand All @@ -51,7 +50,7 @@ export function activate(context: vscode.ExtensionContext): void {
}
}

function registerRenameCommand(provider: ZettelViewTreeDataProvider, workspaceRoot: string, incomingLinksMap: IncomingLinksMap) {
function registerRenameCommand(provider: ZettelViewTreeDataProvider, workspaceRoot: string, incomingIDMap: IncomingIDMap) {
vscode.commands.registerCommand('zettelView.renameEntry', async (node) => {
// Prompt the user for the new name
const newID = await vscode.window.showInputBox({ prompt: 'Enter the new ID' });
Expand Down Expand Up @@ -80,7 +79,7 @@ function registerRenameCommand(provider: ZettelViewTreeDataProvider, workspaceRo
const oldID = path.basename(oldPath, '.md'); // Extract old ID from oldPath without '.md'

// Call replaceIncomingLinks function
await replaceIncomingLinks(oldID, newID, workspaceRoot, incomingLinksMap);
await replaceIncomingIDs(oldID, newID, workspaceRoot, incomingIDMap);

// Refresh the tree view
provider.refresh();
Expand Down
18 changes: 9 additions & 9 deletions src/tests/updateBacklinksMap.test.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import { expect } from 'chai';
import { IncomingLinksMap } from '../utils/IncomingLinksMap';
import { updateIncomingLinksMap } from '../utils/updateIncomingLinksMap';
import { IncomingIDMap } from '../utils/IncomingIDMap';
import { updateIncomingIDMap } from '../utils/updateIncomingIDMap';


describe('updateBacklinksMap', () => {
let map: IncomingLinksMap;
describe('updateIncomingIDMap', () => {
let map: IncomingIDMap;

beforeEach(() => {
map = new IncomingLinksMap();
map = new IncomingIDMap();
});

it('should update the incoming links map correctly', async () => {
map.addLink('oldID', 'sourceID');
await updateIncomingLinksMap('oldID', 'newID', map);
expect(map.getIncomingLinksFor('sourceID')).to.include('newID');
expect(map.getIncomingLinksFor('sourceID')).to.not.include('oldID');
map.addID('oldID', 'sourceID');
await updateIncomingIDMap('oldID', 'newID', map);
expect(map.getIncomingIDsFor('sourceID')).to.include('newID');
expect(map.getIncomingIDsFor('sourceID')).to.not.include('oldID');
});

// Add more tests as needed...
Expand Down
34 changes: 34 additions & 0 deletions src/utils/IncomingIDMap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
export class IncomingIDMap {
private incomingIDs: Map<string, Set<string>>;

constructor() {
this.incomingIDs = new Map<string, Set<string>>();
}

addID(sourceID: string, targetID: string) {
if (!this.incomingIDs.has(targetID)) {
this.incomingIDs.set(targetID, new Set<string>());
}

this.incomingIDs.get(targetID)?.add(sourceID);
}

removeID(sourceID: string, targetID: string) {
const incomingIDsForTarget = this.incomingIDs.get(targetID);
if (incomingIDsForTarget) {
incomingIDsForTarget.delete(sourceID);

if (incomingIDsForTarget.size === 0) {
this.incomingIDs.delete(targetID);
}
}
}

getIncomingIDsFor(targetID: string): Set<string> {
return this.incomingIDs.get(targetID) || new Set<string>();
}

// And so on
}

export const incomingIDMap = new IncomingIDMap();
Loading

0 comments on commit 98de52c

Please sign in to comment.