Skip to content
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
11 changes: 11 additions & 0 deletions lib/autocomplete-paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export function activate() {
})
)

// TODO should we use separate observers for each config?
const cacheOptions = [
"core.ignoredNames",
"core.excludeVcsIgnoredPaths",
Expand Down Expand Up @@ -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() {
Expand Down
9 changes: 5 additions & 4 deletions lib/paths-cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>} */
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
}
Expand Down
26 changes: 16 additions & 10 deletions lib/paths-provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export default class PathsProvider extends EventEmitter {
constructor() {
super()
this.reloadScopes()
this.updateConfig()

this._pathsCache = new PathsCache()
this._isReady = false
Expand All @@ -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
*/
Expand Down Expand Up @@ -122,39 +130,37 @@ 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) {
const regex = new RegExp(`.(${extensions.join("|")})$`)
files = files.filter((path) => regex.test(path))
}

const fuzzyMatcher = directoryGiven ? parsedPathPrefix.base : pathPrefix
if (fuzzyMatcher) {
files = filter(files, fuzzyMatcher, {
maxResults: 10,
})
}

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) {
Expand Down Expand Up @@ -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() {
Expand Down