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

wip: feat: Make foam respect the .gitignore file #1413

Draft
wants to merge 3 commits into
base: master
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
9 changes: 8 additions & 1 deletion packages/foam-vscode/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export async function activate(context: ExtensionContext) {
exposeLogger(context, logger);

try {
Logger.info('Starting Foam');
Logger.info('[wtw] Starting Foam');

if (workspace.workspaceFolders === undefined) {
Logger.info('No workspace open. Foam will not start');
Expand Down Expand Up @@ -100,6 +100,13 @@ export async function activate(context: ExtensionContext) {
'Foam: Reload the window to use the updated settings'
);
}
}),
// 2024-11-25 21:22:48 TODO wip not working as expected
workspace.createFileSystemWatcher('.gitignore').onDidChange(e => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to live inside context.subscriptions so that it's properly cleaned up afterwards

Logger.info(`[wtw] File changed: ${e.fsPath}`);
window.showInformationMessage(
'Foam: Reload the window to use the updated .gitignore settings'
);
Comment on lines +103 to +109
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@riccardoferretti Hello, based on online searches and AI suggestions, I believe that this approach should be able to achieve the effect of issuing a prompt when modifying the ignore settings. However, when I tested it, I found that it did not take effect. I would like to consult with you to see if you have any experience and can help me identify where I might have gone wrong.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that looks like it should work, I am not really sure why it doesn't...

})
);

Expand Down
27 changes: 26 additions & 1 deletion packages/foam-vscode/src/services/editor.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isEmpty } from 'lodash';
import { isEmpty, map, compact, filter, split, startsWith } from 'lodash';
import {
EndOfLine,
FileType,
Expand All @@ -25,6 +25,8 @@ import {
IDataStore,
IMatcher,
} from '../core/services/datastore';
import * as path from 'path';
import { Logger } from '../core/utils/log';

interface SelectionInfo {
document: TextDocument;
Expand Down Expand Up @@ -201,6 +203,29 @@ export async function createMatcherAndDataStore(excludes: string[]): Promise<{
const excludePatterns = new Map<string, string[]>();
workspace.workspaceFolders.forEach(f => excludePatterns.set(f.name, []));

Logger.info('[wtw] Excluded patterns from settings: ' + excludes);
// Read .gitignore files and add patterns to excludePatterns
for (const folder of workspace.workspaceFolders) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this block of code should live here, I would have it together with the rest of the configuration, in order to populate the excludePatterns variable, which is then fed into this fn.

const gitignoreUri = Uri.joinPath(folder.uri, '.gitignore');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This path needs to be the same as the file(s) you are watching for changes. (this should be easy to do if you have both at the top level).
I would expect something like this in extension.ts:

  1. compute .gitignore path(s)
  2. extract exclude patterns from those
  3. merge them with the regular configuration settings and use them in this fn
  4. monitor the paths computed

try {
await workspace.fs.stat(gitignoreUri); // Check if the file exists
const gitignoreContent = await workspace.fs.readFile(gitignoreUri); // Read the file content
const patterns = map(
filter(
split(Buffer.from(gitignoreContent).toString('utf-8'), '\n'),
line => line && !startsWith(line, '#')
),
line => line.trim()
);
excludePatterns.get(folder.name).push(...compact(patterns));

Logger.info(`Excluded patterns from ${gitignoreUri.path}: ${patterns}`);
} catch (error) {
// .gitignore file does not exist, continue
Logger.error(`Error reading .gitignore file: ${error}`);
}
}

for (const exclude of excludes) {
const tokens = exclude.split('/');
const matchesFolder = workspace.workspaceFolders.find(
Expand Down