Skip to content

Commit

Permalink
Add directories list
Browse files Browse the repository at this point in the history
  • Loading branch information
nomasprime committed Feb 16, 2021
1 parent 4e48b9a commit 23940aa
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 6 deletions.
1 change: 1 addition & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Including:
- [x] `buffers` current buffer list.
- [x] `cmdhistory` history of commands.
- [x] `colors` colors schemes.
- [x] `directories` search directories from current cwd.
- [x] `files` search files from current cwd.
- [x] `filetypes` file types.
- [x] `grep` grep text from current cwd.
Expand Down
21 changes: 21 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,27 @@
"type": "string"
}
},
"list.source.directories.command": {
"type": "string",
"default": "",
"description": "Command used for search for directories"
},
"list.source.directories.args": {
"type": "array",
"default": [],
"description": "Arguments for search command",
"items": {
"type": "string"
}
},
"list.source.directories.excludePatterns": {
"type": "array",
"default": [],
"description": "Minimatch patterns that should be excluded.",
"items": {
"type": "string"
}
},
"list.source.files.command": {
"type": "string",
"default": "",
Expand Down
58 changes: 58 additions & 0 deletions src/directories.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { ChildProcess, spawn } from 'child_process'
import { workspace } from 'coc.nvim'
import FilesList, { Task as FilesTask } from './files'
import { executable } from './util'

class Task extends FilesTask {
protected post_process_hook(process: ChildProcess) {
let post_process = spawn('sh', ['-c', 'xargs -I {} gdirname {} | sort | uniq'])
this.processes.push(post_process)
post_process.on('error', e => {
this.emit('error', e.message)
})
process.stdout.pipe(post_process.stdin)

return post_process.stdout
}
}

export default class DirectoriesList extends FilesList {
public task = Task
public readonly name = 'directories'
public description = 'Search directories by rg or ag'
public readonly detail = `Install ripgrep in your $PATH to have best experience.
Directories are searched from current cwd by default.
Provide directory names as arguments to search other directories.
Use 'list.source.directories.command' configuration for custom search command.
Use 'list.source.directories.args' configuration for custom command arguments.
Note that rg ignore hidden directories by default.`
public options = [{
name: '-F, -folder',
description: 'Search directories from current workspace folder instead of cwd.'
}, {
name: '-W, -workspace',
description: 'Search directories from all workspace folders instead of cwd.'
}]

public getCommand(): { cmd: string, args: string[] } {
let config = workspace.getConfiguration('list.source.directories')
let cmd = config.get<string>('command', '')
let args = config.get<string[]>('args', [])
if (!cmd) {
if (executable('rg')) {
return { cmd: 'rg', args: this.getArgs(args, ['--color', 'never', '--files']) }
} else if (executable('ag')) {
return { cmd: 'ag', args: this.getArgs(args, ['-f', '-g', '.', '--nocolor']) }
} else if (process.platform == 'win32') {
return { cmd: 'dir', args: this.getArgs(args, ['/a-D', '/S', '/B']) }
} else if (executable('find')) {
return { cmd: 'find', args: this.getArgs(args, ['.', '-type', 'f']) }
} else {
throw new Error('Unable to find command for directories list.')
return null
}
} else {
return { cmd, args }
}
}
}
17 changes: 11 additions & 6 deletions src/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import path from 'path'
import readline from 'readline'
import { executable } from './util'

class Task extends EventEmitter implements ListTask {
private processes: ChildProcess[] = []
export class Task extends EventEmitter implements ListTask {
protected processes: ChildProcess[] = []

public start(cmd: string, args: string[], cwds: string[], patterns: string[]): void {
let remain = cwds.length
Expand All @@ -18,7 +18,7 @@ class Task extends EventEmitter implements ListTask {
process.on('error', e => {
this.emit('error', e.message)
})
const rl = readline.createInterface(process.stdout)
const rl = readline.createInterface(this.post_process_hook(process))
const range = Range.create(0, 0, 0, 0)
let hasPattern = patterns.length > 0
process.stderr.on('data', chunk => {
Expand Down Expand Up @@ -54,13 +54,18 @@ class Task extends EventEmitter implements ListTask {
}
}
}

protected post_process_hook(process: ChildProcess) {
return process.stdout
}
}

export default class FilesList extends BasicList {
public readonly name = 'files'
public task = Task
public name = 'files'
public readonly defaultAction = 'open'
public description = 'Search files by rg or ag'
public readonly detail = `Install ripgrep in your $PATH to have best experience.
public detail = `Install ripgrep in your $PATH to have best experience.
Files is searched from current cwd by default.
Provide directory names as arguments to search other directories.
Use 'list.source.files.command' configuration for custom search command.
Expand Down Expand Up @@ -147,7 +152,7 @@ Note that rg ignore hidden files by default.`
}
}
}
let task = new Task()
let task = new this.task()
let excludePatterns = this.getConfig().get<string[]>('excludePatterns', [])
task.start(res.cmd, res.args.concat(searchArgs), cwds, excludePatterns)
return task
Expand Down
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import BufferList from './buffers'
import Cmdhistory from './cmdhistory'
import Colors from './colors'
import Commands from './commands'
import DirectoriesList from './directories'
import FilesList from './files'
import Filetypes from './filetypes'
import GrepList from './grep'
Expand Down Expand Up @@ -58,6 +59,9 @@ export async function activate(context: ExtensionContext): Promise<void> {
if (!isDisabled('filetypes')) {
subscriptions.push(listManager.registerList(new Filetypes(nvim)))
}
if (!isDisabled('directories')) {
subscriptions.push(listManager.registerList(new DirectoriesList(nvim)))
}
if (!isDisabled('files')) {
subscriptions.push(listManager.registerList(new FilesList(nvim)))
}
Expand Down

0 comments on commit 23940aa

Please sign in to comment.