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

feat(workspaceFolder): support wildcard for bottomUpFiletypes #3854

Merged
merged 4 commits into from
May 28, 2022
Merged
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
2 changes: 1 addition & 1 deletion data/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -1071,7 +1071,7 @@
"workspace.bottomUpFiletypes": {
"type": "array",
"default": [],
"description": "Filetypes that should have workspace folder should resolved from base directory of file.",
"description": "Filetypes that should have workspace folder should resolved from base directory of file, or [\"*\"] for any filetype.",
"items": {
"type": "string"
}
Expand Down
2 changes: 1 addition & 1 deletion doc/coc.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1432,7 +1432,7 @@ Workspace related~
"workspace.bottomUpFiletypes" *coc-config-workspace-bottomUpFiletypes*

Filetypes that should have workspace folder should resolved from
base directory of file.
base directory of file, or `["*"]` for any filetype.

Default: []

Expand Down
36 changes: 36 additions & 0 deletions src/__tests__/core/workspaceFolder.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Neovim } from '@chemzqm/neovim'
import fs from 'fs'
import os from 'os'
import path from 'path'
import { Disposable, WorkspaceFoldersChangeEvent } from 'vscode-languageserver-protocol'
Expand Down Expand Up @@ -221,6 +222,41 @@ describe('WorkspaceFolderController', () => {
let res = workspaceFolder.resolveRoot(doc, path.join(os.homedir(), 'foo'), true, expand)
expect(res).toBe(null)
})

describe('bottomUpFileTypes', () => {
it('should respect specific filetype', async () => {
updateConfiguration('coc.preferences.rootPatterns', ['.vim'], ['.git', '.hg', '.projections.json'])
updateConfiguration('workspace.bottomUpFiletypes', ['vim'], [])
let root = path.join(os.tmpdir(), 'a')
let dir = path.join(root, '.vim')
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, {recursive: true})
await helper.wait(30)
}
let file = path.join(dir, 'foo')
await nvim.command(`edit ${file}`)
await nvim.command('setf vim')
let doc = await workspace.document
let res = workspaceFolder.resolveRoot(doc, file, true, expand)
expect(res).toBe(root)
})

it('should respect wildcard', async () => {
updateConfiguration('coc.preferences.rootPatterns', ['.vim'], ['.git', '.hg', '.projections.json'])
updateConfiguration('workspace.bottomUpFiletypes', ['*'], [])
let root = path.join(os.tmpdir(), 'a')
let dir = path.join(root, '.vim')
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, {recursive: true})
await helper.wait(30)
}
let file = path.join(dir, 'foo')
await nvim.command(`edit ${file}`)
let doc = await workspace.document
let res = workspaceFolder.resolveRoot(doc, file, true, expand)
expect(res).toBe(root)
})
})
})

describe('renameWorkspaceFolder()', () => {
Expand Down
4 changes: 2 additions & 2 deletions src/core/workspaceFolder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export default class WorkspaceFolderController {
let dir = path.dirname(u.fsPath)
let config = this.configurations.getConfiguration('workspace', document.uri)
let ignoredFiletypes = config.get<string[]>('ignoredFiletypes', [])
let bottomUpFileTypes = config.get<string[]>('bottomUpFiletypes', [])
let bottomUpFiletypes = config.get<string[]>('bottomUpFiletypes', [])
let checkCwd = config.get<boolean>('workspaceFolderCheckCwd', true)
let ignored = config.get<string[]>('ignoredFolders', [])
let fallbackCwd = config.get<boolean>('workspaceFolderFallbackCwd', true)
Expand All @@ -99,7 +99,7 @@ export default class WorkspaceFolderController {
for (let patternType of types) {
let patterns = this.getRootPatterns(document, patternType)
if (patterns && patterns.length) {
let isBottomUp = bottomUpFileTypes.includes(document.filetype)
let isBottomUp = bottomUpFiletypes.includes('*') || bottomUpFiletypes.includes(document.filetype)
let root = resolveRoot(dir, patterns, cwd, isBottomUp, checkCwd, ignored)
if (root) {
res = root
Expand Down