Skip to content

Commit

Permalink
feat(list): formatFilepath works for location list
Browse files Browse the repository at this point in the history
Related #5243
  • Loading branch information
chemzqm committed Feb 26, 2025
1 parent 883ae3d commit 1f0431d
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 77 deletions.
29 changes: 15 additions & 14 deletions doc/coc.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3899,6 +3899,21 @@ location *coc-list-location*
- 'split': Use |:split| to open location.
- 'quickfix': Add selected items to Vim's quickfix.

To customize filepath displayed in the list, user could inject
javascript global function `formatFilepath` which accept filepath and
should return string. ex:
>
const path = require('path')
global.formatFilepath = function (file) {
return file.startsWith('jdt:/') ? path.basename(file) : file
}
<
save the file to `~/custom.js` and make coc load it by add
>
let g:coc_node_args = ['-r', expand('~/custom.js')]
<
to your vimrc. `formatFilepath` works for |coc-list-symbols| as well.

extensions *coc-list-extensions*

Manage coc.nvim extensions.
Expand Down Expand Up @@ -3949,20 +3964,6 @@ outline *coc-list-outline*
symbols *coc-list-symbols*

Search workspace symbols.
To customize filepath displayed in the list, user could inject
javascript global function `formatFilepath` which accept filepath and
should return string. ex:
>
const path = require('path')
global.formatFilepath = function (file) {
return file.startsWith('jdt:/') ? path.basename(file) : file
}
<
save the file to `~/custom.js` and make coc load it by add
>
let g:coc_node_args = ['-r', expand('~/custom.js')]
<
to your vimrc

Actions:

Expand Down
19 changes: 7 additions & 12 deletions src/__tests__/list/sources.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import ExtensionList, { getExtensionPrefix, getExtensionPriority, sortExtensionI
import FolderList from '../../list/source/folders'
import { mruScore } from '../../list/source/lists'
import OutlineList, { contentToItems, getFilterText, loadCtagsSymbols, symbolsToListItems } from '../../list/source/outline'
import SymbolsList, { sortSymbolItems, formatFilepath } from '../../list/source/symbols'
import SymbolsList, { sortSymbolItems } from '../../list/source/symbols'
import { ListArgument, ListContext, ListItem, ListOptions } from '../../list/types'
import Document from '../../model/document'
import services, { IServiceProvider, ServiceStat } from '../../services'
Expand Down Expand Up @@ -161,15 +161,6 @@ describe('formatting', () => {
expect(fixWidth('a'.repeat(10), 2)).toBe('a.')
})

it('should support formatFilepath', () => {
global.formatFilepath = function() {
return ''
}
let res = formatFilepath('foo')
expect(res).toBe('')
global.formatFilepath = undefined
})

it('should sort symbols', () => {
const assert = (a, b, n) => {
expect(sortSymbolItems(a, b)).toBe(n)
Expand Down Expand Up @@ -536,11 +527,15 @@ describe('list sources', () => {
})

it('should do open action', async () => {
global.formatFilepath = function() {
return ''
}
await manager.start(['--normal', 'location'])
await manager.session.ui.ready
await manager.doAction('open')
let name = await nvim.eval('bufname("%")')
expect(name).toMatch('sources.test.ts')
global.formatFilepath = undefined
})

it('should do quickfix action', async () => {
Expand Down Expand Up @@ -705,7 +700,7 @@ describe('list sources', () => {

const workspaceFolder = path.join(__dirname, 'workspace-folder1')
jest.spyOn(workspace, 'getWorkspaceFolder').mockReturnValue({
name : 'workspace-folder1',
name: 'workspace-folder1',
uri: URI.file(workspaceFolder).toString()
})
await manager.start(['diagnostics', '--workspace-folder'])
Expand All @@ -730,7 +725,7 @@ describe('list sources', () => {

const workspaceFolder = path.join(__dirname, 'workspace-folder4')
jest.spyOn(workspace, 'getWorkspaceFolder').mockReturnValue({
name : 'workspace-folder4',
name: 'workspace-folder4',
uri: URI.file(workspaceFolder).toString()
})
await manager.start(['diagnostics', '--workspace-folder'])
Expand Down
94 changes: 51 additions & 43 deletions src/list/source/location.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ export default class LocationList extends BasicList {
this.addLocationActions()
}

public formatFilepath(file: string): string {
if (typeof global.formatFilepath === 'function') {
return global.formatFilepath(file) + ''
}
return file
}

public async loadItems(context: ListContext, _token: CancellationToken): Promise<ListItem[]> {
// filename, lnum, col, text, type
let locs = await this.nvim.getVar('coc_jump_locations') as QuickfixItem[]
Expand All @@ -40,52 +47,53 @@ export default class LocationList extends BasicList {
if (filename.length > 0 && path.isAbsolute(filename)) {
filename = isParentFolder(context.cwd, filename) ? path.relative(context.cwd, filename) : filename
}
return createItem(filename, loc)
return this.createItem(filename, loc)
})
return items
}
}

function createItem(filename: string, loc: QuickfixItem): ListItem {
let uri = loc.uri ?? URI.file(loc.filename).toString()
let label = ''
const ansiHighlights: AnsiHighlight[] = []
let start = 0
if (filename.length > 0) {
label = filename + ' '
ansiHighlights.push({ span: [start, start + byteLength(filename)], hlGroup: 'Directory' })
}
start = byteLength(label)
let lnum = loc.lnum ?? loc.range.start.line + 1
let col = loc.col ?? byteLength(loc.text.slice(0, loc.range.start.character)) + 1
let position = `|${loc.type ? loc.type + ' ' : ''}${lnum} Col ${col}|`
label += position
ansiHighlights.push({ span: [start, start + byteLength(position)], hlGroup: 'LineNr' })
if (loc.type) {
let hl = loc.type.toLowerCase() === 'error' ? 'Error' : 'WarningMsg'
ansiHighlights.push({ span: [start + 1, start + byteLength(loc.type)], hlGroup: hl })
}
if (loc.range && loc.range.start.line == loc.range.end.line) {
let len = byteLength(label) + 1
let start = len + byteLength(loc.text.slice(0, loc.range.start.character))
let end = len + byteLength(loc.text.slice(0, loc.range.end.character))
ansiHighlights.push({ span: [start, end], hlGroup: 'Search' })
}
label += ' ' + loc.text
let filterText = `${filename}${loc.text.trim()}`
let location: LocationWithTarget
if (loc.range) {
location = Location.create(uri, loc.range)
} else {
let start = Position.create(loc.lnum - 1, loc.col - 1)
let end = Position.create((loc.end_lnum ?? loc.lnum) - 1, (loc.end_col ?? loc.col) - 1)
location = Location.create(uri, Range.create(start, end))
}
location.targetRange = loc.targetRange ? loc.targetRange : Range.create(lnum - 1, 0, lnum - 1, 99)
return {
label,
location,
filterText,
ansiHighlights,
private createItem(filename: string, loc: QuickfixItem): ListItem {
let uri = loc.uri ?? URI.file(loc.filename).toString()
let label = ''
const ansiHighlights: AnsiHighlight[] = []
let start = 0
filename = this.formatFilepath(filename)
if (filename.length > 0) {
label = filename + ' '
ansiHighlights.push({ span: [start, start + byteLength(filename)], hlGroup: 'Directory' })
}
start = byteLength(label)
let lnum = loc.lnum ?? loc.range.start.line + 1
let col = loc.col ?? byteLength(loc.text.slice(0, loc.range.start.character)) + 1
let position = `|${loc.type ? loc.type + ' ' : ''}${lnum} Col ${col}|`
label += position
ansiHighlights.push({ span: [start, start + byteLength(position)], hlGroup: 'LineNr' })
if (loc.type) {
let hl = loc.type.toLowerCase() === 'error' ? 'Error' : 'WarningMsg'
ansiHighlights.push({ span: [start + 1, start + byteLength(loc.type)], hlGroup: hl })
}
if (loc.range && loc.range.start.line == loc.range.end.line) {
let len = byteLength(label) + 1
let start = len + byteLength(loc.text.slice(0, loc.range.start.character))
let end = len + byteLength(loc.text.slice(0, loc.range.end.character))
ansiHighlights.push({ span: [start, end], hlGroup: 'Search' })
}
label += ' ' + loc.text
let filterText = `${filename}${loc.text.trim()}`
let location: LocationWithTarget
if (loc.range) {
location = Location.create(uri, loc.range)
} else {
let start = Position.create(loc.lnum - 1, loc.col - 1)
let end = Position.create((loc.end_lnum ?? loc.lnum) - 1, (loc.end_col ?? loc.col) - 1)
location = Location.create(uri, Range.create(start, end))
}
location.targetRange = loc.targetRange ? loc.targetRange : Range.create(lnum - 1, 0, lnum - 1, 99)
return {
label,
location,
filterText,
ansiHighlights,
}
}
}
9 changes: 1 addition & 8 deletions src/list/source/symbols.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,6 @@ interface ItemToSort {
}
}

export function formatFilepath(file: string): string {
if (typeof global.formatFilepath === 'function') {
return global.formatFilepath(file) + ''
}
return file
}

export default class Symbols extends LocationList {
public readonly interactive = true
public readonly description = 'search workspace symbols'
Expand Down Expand Up @@ -88,7 +81,7 @@ export default class Symbols extends LocationList {
let label = ''
let ansiHighlights: AnsiHighlight[] = []
// Normal Typedef Comment
let parts = [name, `[${kind}]`, formatFilepath(file)]
let parts = [name, `[${kind}]`, this.formatFilepath(file)]
let highlights = ['Normal', 'Typedef', 'Comment']
for (let index = 0; index < parts.length; index++) {
const text = parts[index]
Expand Down

0 comments on commit 1f0431d

Please sign in to comment.