Skip to content

Commit

Permalink
Passing a reference to the source link to the create-note command
Browse files Browse the repository at this point in the history
So it can update it if necessary
  • Loading branch information
riccardoferretti committed Mar 17, 2024
1 parent 3f509e1 commit 699030f
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 35 deletions.
51 changes: 49 additions & 2 deletions packages/foam-vscode/src/features/commands/create-note.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ import {
showInEditor,
} from '../../test/test-utils-vscode';
import { fromVsCodeUri } from '../../utils/vsc-utils';
import { CREATE_NOTE_COMMAND } from './create-note';
import { CREATE_NOTE_COMMAND, createNote } from './create-note';
import { Location } from '../../core/model/location';
import { Range } from '../../core/model/range';
import { ResourceLink } from '../../core/model/note';
import { MarkdownResourceProvider } from '../../core/services/markdown-provider';
import { createMarkdownParser } from '../../core/services/markdown-parser';

describe('create-note command', () => {
afterEach(() => {
Expand Down Expand Up @@ -194,8 +199,14 @@ describe('factories', () => {
describe('forPlaceholder', () => {
it('adds the .md extension to notes created for placeholders', async () => {
await closeEditors();
const link: ResourceLink = {
type: 'wikilink',
rawText: '[[my-placeholder]]',
range: Range.create(0, 0, 0, 0),
isEmbed: false,
};
const command = CREATE_NOTE_COMMAND.forPlaceholder(
'my-placeholder',
Location.forObjectWithRange(URI.file(''), link),
'.md'
);
await commands.executeCommand(command.name, command.params);
Expand All @@ -204,5 +215,41 @@ describe('factories', () => {
expect(doc.uri.path).toMatch(/my-placeholder.md$/);
expect(doc.getText()).toMatch(/^# my-placeholder/);
});

it('replaces the original placeholder based on the new note identifier (#1327)', async () => {
await closeEditors();
const templateA = await createFile(
`---
foam_template:
name: 'Example Template'
description: 'An example for reproducing a bug'
filepath: '$FOAM_SLUG-world.md'
---`,
['.foam', 'templates', 'template-a.md']
);

const noteA = await createFile(`this is my [[hello]]`);

const parser = createMarkdownParser();
const res = parser.parse(noteA.uri, noteA.content);

const command = CREATE_NOTE_COMMAND.forPlaceholder(
Location.forObjectWithRange(noteA.uri, res.links[0]),
'.md',
{
templatePath: templateA.uri.path,
}
);
const results: Awaited<ReturnType<typeof createNote>> =
await commands.executeCommand(command.name, command.params);
expect(results.didCreateFile).toBeTruthy();
expect(results.uri.path.endsWith('hello-world.md')).toBeTruthy();

const newNoteDoc = window.activeTextEditor.document;
expect(newNoteDoc.uri.path).toMatch(/hello-world.md$/);

const { doc } = await showInEditor(noteA.uri);
expect(doc.getText()).toEqual(`this is my [[hello-world]]`);
});
});
});
75 changes: 54 additions & 21 deletions packages/foam-vscode/src/features/commands/create-note.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,21 @@ import { Resolver } from '../../services/variable-resolver';
import { asAbsoluteWorkspaceUri, fileExists } from '../../services/editor';
import { isSome } from '../../core/utils';
import { CommandDescriptor } from '../../utils/commands';
import { Foam } from '../../core/model/foam';
import { Location } from '../../core/model/location';
import { MarkdownLink } from '../../core/services/markdown-link';
import { ResourceLink } from '../../core/model/note';
import { toVsCodeRange, toVsCodeUri } from '../../utils/vsc-utils';

export default async function activate(context: vscode.ExtensionContext) {
export default async function activate(
context: vscode.ExtensionContext,
foamPromise: Promise<Foam>
) {
const foam = await foamPromise;
context.subscriptions.push(
vscode.commands.registerCommand(CREATE_NOTE_COMMAND.command, createNote)
vscode.commands.registerCommand(CREATE_NOTE_COMMAND.command, args =>
createNote(args, foam)
)
);
}

Expand Down Expand Up @@ -48,6 +59,11 @@ interface CreateNoteArgs {
* The title of the note (translates into the FOAM_TITLE variable)
*/
title?: string;
/**
* The source link that triggered the creation of the note.
* It will be updated with the appropriate identifier to the note, if necessary.
*/
sourceLink?: Location<ResourceLink>;
/**
* What to do in case the target file already exists
*/
Expand All @@ -66,7 +82,7 @@ const DEFAULT_NEW_NOTE_TEXT = `# \${FOAM_TITLE}
\${FOAM_SELECTED_TEXT}`;

async function createNote(args: CreateNoteArgs) {
export async function createNote(args: CreateNoteArgs, foam: Foam) {
args = args ?? {};
const date = isSome(args.date) ? new Date(Date.parse(args.date)) : new Date();
const resolver = new Resolver(
Expand All @@ -92,23 +108,39 @@ async function createNote(args: CreateNoteArgs) {
: getDefaultTemplateUri();
}

if (await fileExists(templateUri)) {
return NoteFactory.createFromTemplate(
templateUri,
resolver,
noteUri,
text,
args.onFileExists
);
} else {
return NoteFactory.createNote(
noteUri ?? (await getPathFromTitle(resolver)),
text,
resolver,
args.onFileExists,
args.onRelativeNotePath
);
const createdNote = (await fileExists(templateUri))
? await NoteFactory.createFromTemplate(
templateUri,
resolver,
noteUri,
text,
args.onFileExists
)
: await NoteFactory.createNote(
noteUri ?? (await getPathFromTitle(resolver)),
text,
resolver,
args.onFileExists,
args.onRelativeNotePath
);

if (args.sourceLink) {
const identifier = foam.workspace.getIdentifier(createdNote.uri);
const edit = MarkdownLink.createUpdateLinkEdit(args.sourceLink.data, {
target: identifier,
});
if (edit.newText != args.sourceLink.data.rawText) {
const updateLink = new vscode.WorkspaceEdit();
const uri = toVsCodeUri(args.sourceLink.uri);
updateLink.replace(
uri,
toVsCodeRange(args.sourceLink.range),
edit.newText
);
await vscode.workspace.applyEdit(updateLink);
}
}
return createdNote;
}

export const CREATE_NOTE_COMMAND = {
Expand All @@ -123,12 +155,12 @@ export const CREATE_NOTE_COMMAND = {
* @returns the command descriptor
*/
forPlaceholder: (
placeholder: string,
sourceLink: Location<ResourceLink>,
defaultExtension: string,
extra: Partial<CreateNoteArgs> = {}
): CommandDescriptor<CreateNoteArgs> => {
const endsWithDefaultExtension = new RegExp(defaultExtension + '$');

const { target: placeholder } = MarkdownLink.analyzeLink(sourceLink.data);
const title = placeholder.endsWith(defaultExtension)
? placeholder.replace(endsWithDefaultExtension, '')
: placeholder;
Expand All @@ -140,6 +172,7 @@ export const CREATE_NOTE_COMMAND = {
params: {
title,
notePath,
sourceLink,
...extra,
},
};
Expand Down
3 changes: 2 additions & 1 deletion packages/foam-vscode/src/features/hover-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { FoamGraph } from '../core/model/graph';
import { OPEN_COMMAND } from './commands/open-resource';
import { CREATE_NOTE_COMMAND } from './commands/create-note';
import { commandAsURI } from '../utils/commands';
import { Location } from '../core/model/location';

export const CONFIG_KEY = 'links.hover.enable';

Expand Down Expand Up @@ -107,7 +108,7 @@ export class HoverProvider implements vscode.HoverProvider {
}

const command = CREATE_NOTE_COMMAND.forPlaceholder(
targetUri.path,
Location.forObjectWithRange(documentUri, targetLink),
this.workspace.defaultExtension,
{
askForTemplate: true,
Expand Down
19 changes: 13 additions & 6 deletions packages/foam-vscode/src/features/navigation-provider.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ import { createMarkdownParser } from '../core/services/markdown-parser';
import { FoamGraph } from '../core/model/graph';
import { commandAsURI } from '../utils/commands';
import { CREATE_NOTE_COMMAND } from './commands/create-note';
import { Location } from '../core/model/location';
import { URI } from '../core/model/uri';
import { Range } from '../core/model/range';
import { ResourceLink } from '../core/model/note';

describe('Document navigation', () => {
const parser = createMarkdownParser([]);
Expand Down Expand Up @@ -71,9 +75,8 @@ describe('Document navigation', () => {

it('should create links for placeholders', async () => {
const fileA = await createFile(`this is a link to [[a placeholder]].`);
const ws = createTestWorkspace().set(
parser.parse(fileA.uri, fileA.content)
);
const noteA = parser.parse(fileA.uri, fileA.content);
const ws = createTestWorkspace().set(noteA);
const graph = FoamGraph.fromWorkspace(ws);

const { doc } = await showInEditor(fileA.uri);
Expand All @@ -83,9 +86,13 @@ describe('Document navigation', () => {
expect(links.length).toEqual(1);
expect(links[0].target).toEqual(
commandAsURI(
CREATE_NOTE_COMMAND.forPlaceholder('a placeholder', '.md', {
onFileExists: 'open',
})
CREATE_NOTE_COMMAND.forPlaceholder(
Location.forObjectWithRange(noteA.uri, noteA.links[0]),
'.md',
{
onFileExists: 'open',
}
)
)
);
expect(links[0].range).toEqual(new vscode.Range(0, 20, 0, 33));
Expand Down
9 changes: 4 additions & 5 deletions packages/foam-vscode/src/features/navigation-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { FoamGraph } from '../core/model/graph';
import { Position } from '../core/model/position';
import { CREATE_NOTE_COMMAND } from './commands/create-note';
import { commandAsURI } from '../utils/commands';
import { Location } from '../core/model/location';

export default async function activate(
context: vscode.ExtensionContext,
Expand Down Expand Up @@ -146,10 +147,8 @@ export class NavigationProvider
public provideDocumentLinks(
document: vscode.TextDocument
): vscode.DocumentLink[] {
const resource = this.parser.parse(
fromVsCodeUri(document.uri),
document.getText()
);
const documentUri = fromVsCodeUri(document.uri);
const resource = this.parser.parse(documentUri, document.getText());

const targets: { link: ResourceLink; target: URI }[] = resource.links.map(
link => ({
Expand All @@ -162,7 +161,7 @@ export class NavigationProvider
.filter(o => o.target.isPlaceholder()) // links to resources are managed by the definition provider
.map(o => {
const command = CREATE_NOTE_COMMAND.forPlaceholder(
o.target.path,
Location.forObjectWithRange(documentUri, o.link),
this.workspace.defaultExtension,
{
onFileExists: 'open',
Expand Down

0 comments on commit 699030f

Please sign in to comment.