diff --git a/lib/autocomplete-paths.js b/lib/autocomplete-paths.js index da84ade..0eea2ee 100644 --- a/lib/autocomplete-paths.js +++ b/lib/autocomplete-paths.js @@ -21,6 +21,7 @@ export function activate() { }) ) + // TODO should we use separate observers for each config? const cacheOptions = [ "core.ignoredNames", "core.excludeVcsIgnoredPaths", @@ -51,6 +52,16 @@ export function activate() { }) ) }) + + const pathsProviderConfigs = ["autocomplete-paths.normalizeSlashes", "autocomplete-paths.suggestionPriority"] + pathsProviderConfigs.forEach((pathsProviderConfig) => { + subscriptions.add( + atom.config.observe(pathsProviderConfig, () => { + if (!_provider) return + _provider.updateConfig() + }) + ) + }) } export function deactivate() { diff --git a/lib/paths-cache.js b/lib/paths-cache.js index 0283844..fc02491 100644 --- a/lib/paths-cache.js +++ b/lib/paths-cache.js @@ -55,13 +55,14 @@ export default class PathsCache extends EventEmitter { /** * Returns the file paths for the given project directory with the given (optional) relative path * @param {Directory} projectDirectory - * @param {String} [relativeToPath=null] + * @param {string} [relativeToPath=undefined] * @return {String[]} */ - getFilePathsForProjectDirectory(projectDirectory, relativeToPath = null) { - let filePaths = this._filePathsByProjectDirectory.get(projectDirectory.path) || [] + getFilePathsForProjectDirectory(projectDirectory, relativeToPath = undefined) { + /** @type {Array} */ + const filePaths = this._filePathsByProjectDirectory.get(projectDirectory.path) || [] if (relativeToPath) { - return filePaths.filter((filePath) => filePath.indexOf(relativeToPath) === 0) + return filePaths.filter((filePath) => filePath.startsWith(relativeToPath)) } return filePaths } diff --git a/lib/paths-provider.js b/lib/paths-provider.js index 2eaa53f..2a2c3a3 100644 --- a/lib/paths-provider.js +++ b/lib/paths-provider.js @@ -13,6 +13,7 @@ export default class PathsProvider extends EventEmitter { constructor() { super() this.reloadScopes() + this.updateConfig() this._pathsCache = new PathsCache() this._isReady = false @@ -24,6 +25,13 @@ export default class PathsProvider extends EventEmitter { this._pathsCache.on("rebuild-cache-done", this._onRebuildCacheDone) } + updateConfig() { + this.config = { + normalizeSlashes: atom.config.get("autocomplete-paths.normalizeSlashes"), + suggestionPriority: atom.config.get("autocomplete-paths.suggestionPriority"), + } + } + /** * Reloads the scopes */ @@ -122,11 +130,10 @@ export default class PathsProvider extends EventEmitter { const requestedDirectoryPath = path.resolve(currentDirectory, parsedPathPrefix.dir) - let files = directoryGiven - ? this._pathsCache.getFilePathsForProjectDirectory(projectDirectory, requestedDirectoryPath) - : this._pathsCache.getFilePathsForProjectDirectory(projectDirectory) - - const fuzzyMatcher = directoryGiven ? parsedPathPrefix.base : pathPrefix + let files = this._pathsCache.getFilePathsForProjectDirectory( + projectDirectory, + directoryGiven ? requestedDirectoryPath : undefined + ) const { extensions } = scope if (extensions) { @@ -134,6 +141,7 @@ export default class PathsProvider extends EventEmitter { files = files.filter((path) => regex.test(path)) } + const fuzzyMatcher = directoryGiven ? parsedPathPrefix.base : pathPrefix if (fuzzyMatcher) { files = filter(files, fuzzyMatcher, { maxResults: 10, @@ -141,20 +149,18 @@ export default class PathsProvider extends EventEmitter { } let suggestions = files.map((pathName) => { - const normalizeSlashes = atom.config.get("autocomplete-paths.normalizeSlashes") - const projectRelativePath = atom.project.relativizePath(pathName)[1] let displayText = projectRelativePath if (directoryGiven) { displayText = path.relative(requestedDirectoryPath, pathName) } - if (normalizeSlashes) { + if (this.config.normalizeSlashes) { displayText = slash(displayText) } // Relativize path to current file if necessary let relativePath = path.relative(path.dirname(request.editor.getPath()), pathName) - if (normalizeSlashes) relativePath = slash(relativePath) + if (this.config.normalizeSlashes) relativePath = slash(relativePath) if (scope.relative !== false) { pathName = relativePath if (scope.includeCurrentDirectory !== false) { @@ -255,7 +261,7 @@ export default class PathsProvider extends EventEmitter { } get suggestionPriority() { - return atom.config.get("autocomplete-paths.suggestionPriority") + return this.config.suggestionPriority } get fileCount() {