diff --git a/packages/foam-vscode/src/extension.ts b/packages/foam-vscode/src/extension.ts index f27bdf604..227829a14 100644 --- a/packages/foam-vscode/src/extension.ts +++ b/packages/foam-vscode/src/extension.ts @@ -1,10 +1,13 @@ /*global markdownit:readonly*/ -import { workspace, ExtensionContext, window, commands } from 'vscode'; +import { workspace, ExtensionContext, window, commands, Uri } from 'vscode'; import { MarkdownResourceProvider } from './core/services/markdown-provider'; import { bootstrap } from './core/model/foam'; import { Logger } from './core/utils/log'; +import * as path from 'path'; +import { Logger } from '../core/utils/log'; +import { isEmpty, map, compact, filter, split, startsWith } from 'lodash'; import { features } from './features'; import { VsCodeOutputLogger, exposeLogger } from './services/logging'; import { @@ -24,7 +27,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'); @@ -33,6 +36,57 @@ export async function activate(context: ExtensionContext) { // Prepare Foam const excludes = getIgnoredFilesSetting().map(g => g.toString()); + + Logger.info('[wtw] Excluded patterns from settings: ' + excludes); + + // Read all .gitignore files and add patterns to excludePatterns + const gitignoreFiles = await workspace.findFiles('**/.gitignore'); + + gitignoreFiles.forEach(gitignoreUri => { + try { + workspace.fs.stat(gitignoreUri); // Check if the file exists + const gitignoreContent = await workspace.fs.readFile(gitignoreUri); + + // TODO maybe better to use a specific gitignore parser lib. + let ignore_rules = Buffer.from(gitignoreContent) + .toString('utf-8') + .split('\n') + .map(line => line.trim()) + .filter(line => !isEmpty(line)) + .filter(line => !line.startsWith('#')) + .forEach(line => excludes.push(line)); + + excludes.push(...ignore_rules); + Logger.info( + `Excluded patterns from ${gitignoreUri.path}: ${ignore_rules}` + ); + } catch (error) { + Logger.error(`Error reading .gitignore file: ${error}`); + } + }); + + // Read .gitignore files and add patterns to excludePatterns + // for (const folder of workspace.workspaceFolders) { + // const gitignoreUri = Uri.joinPath(folder.uri, '.gitignore'); + // 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}`); + // } + // } + const { matcher, dataStore, excludePatterns } = await createMatcherAndDataStore(excludes); @@ -79,6 +133,8 @@ export async function activate(context: ExtensionContext) { const foam = await foamPromise; Logger.info(`Loaded ${foam.workspace.list().length} resources`); + const gitignoreWatcher = workspace.createFileSystemWatcher('**/.gitignore'); + context.subscriptions.push( foam, watcher, @@ -100,9 +156,17 @@ export async function activate(context: ExtensionContext) { 'Foam: Reload the window to use the updated settings' ); } - }) + }), + gitignoreWatcher ); + gitignoreWatcher.onDidChange(e => { + Logger.info(`[wtw] File changed: ${e.fsPath}`); + window.showInformationMessage( + 'Foam: Reload the window to use the updated .gitignore settings' + ); + }); + const feats = (await Promise.all(featuresPromises)).filter(r => r != null); return { diff --git a/packages/foam-vscode/src/services/editor.ts b/packages/foam-vscode/src/services/editor.ts index 5ff0ed19d..64b05a65c 100644 --- a/packages/foam-vscode/src/services/editor.ts +++ b/packages/foam-vscode/src/services/editor.ts @@ -1,4 +1,4 @@ -import { isEmpty } from 'lodash'; +import { isEmpty, map, compact, filter, split, startsWith } from 'lodash'; import { EndOfLine, FileType, @@ -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;