Skip to content

Commit

Permalink
feat(workspace): add createStatusBarItem method
Browse files Browse the repository at this point in the history
  • Loading branch information
chemzqm committed Oct 24, 2018
1 parent 9603202 commit 58ee4d6
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 11 deletions.
2 changes: 1 addition & 1 deletion autoload/coc.vim
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,5 @@ function! coc#status()
if get(info, 'warning', 0)
call add(msgs, '⚠️ ' . info['warning'])
endif
return join(msgs, ' ')
return join(msgs, ' ') . ' ' . get(g:, 'coc_status', '')
endfunction
1 change: 1 addition & 0 deletions autoload/coc/util.vim
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,7 @@ function! coc#util#run_terminal(opts, cb)
let opts = {
\ 'cmd': cmd,
\ 'cwd': get(a:opts, 'cwd', ''),
\ 'keepfocus': get(a:opts, 'keepfocus', 0),
\ 'Callback': {status, bufnr, content -> a:cb(v:null, {'success': status == 0 ? v:true : v:false, 'bufnr': bufnr, 'content': content})}
\}
call coc#util#open_terminal(opts)
Expand Down
2 changes: 1 addition & 1 deletion src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export class CommandManager implements Disposable {
let items = await Promise.all(references.map(loc => {
return workspace.getQuickfixItem(loc)
}))
await nvim.call('setqflist', [[], ' ', { 'title': 'Results of references', items }])
await nvim.call('setqflist', [[], ' ', { title: 'Results of references', items }])
await nvim.command('doautocmd User CocQuickfixChange')
}
}, true)
Expand Down
76 changes: 76 additions & 0 deletions src/model/status.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { Disposable } from 'vscode-languageserver-protocol'
import { NeovimClient as Neovim } from '@chemzqm/neovim'
import { StatusBarItem } from '../types'
import uuidv1 = require('uuid/v1')

const frames = ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏']

export default class StatusLine implements Disposable {
private items: Map<string, StatusBarItem> = new Map()
private shownIds: Set<string> = new Set()
private _text = ''
private interval: NodeJS.Timer
constructor(private nvim: Neovim) {
this.interval = setInterval(() => {
this.setStatusText().catch(_e => {
// noop
})
}, 100)
}

public dispose(): void {
clearInterval(this.interval)
}

public createStatusBarItem(priority = 0, isProgress = false): StatusBarItem {
let uid = uuidv1()

let item: StatusBarItem = {
text: '',
priority,
isProgress,
show: () => {
this.shownIds.add(uid)
},
hide: () => {
this.shownIds.delete(uid)
},
dispose: () => {
this.shownIds.delete(uid)
this.items.delete(uid)
}
}
this.items.set(uid, item)
return item
}

private getText(): string {
if (this.shownIds.size == 0) return ''
let d = new Date()
let idx = Math.floor(d.getMilliseconds() / 100)
let text = ''
let items: StatusBarItem[] = []
for (let [id, item] of this.items) {
if (this.shownIds.has(id)) {
items.push(item)
}
}
items.sort((a, b) => a.priority - b.priority)
for (let item of items) {
if (!item.isProgress) {
text = `${text} ${item.text}`
} else {
text = `${text} ${frames[idx]} ${item.text}`
}
}
return text
}

private async setStatusText(): Promise<void> {
let text = this.getText()
if (text != this._text) {
this._text = text
await this.nvim.setVar('coc_status', text)
}
}
}
4 changes: 2 additions & 2 deletions src/model/terminal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export default class Terminal extends EventEmitter {
return await this.resolveModule(mod)
}

public async runCommand(cmd: string, cwd?: string): Promise<TerminalResult> {
return await this.nvim.callAsync('coc#util#run_terminal', { cmd, cwd }) as TerminalResult
public async runCommand(cmd: string, cwd?: string, keepfocus?: boolean): Promise<TerminalResult> {
return await this.nvim.callAsync('coc#util#run_terminal', { cmd, cwd, keepfocus: keepfocus ? 1 : 0 }) as TerminalResult
}
}
45 changes: 43 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,46 @@ export interface ExtensionInfo {
state: ExtensionState
}

export interface StatusItemOption {
progress?: boolean
}

export interface StatusBarItem {
/**
* The priority of this item. Higher value means the item should
* be shown more to the left.
*/
readonly priority: number

readonly isProgress: boolean

/**
* The text to show for the entry. You can embed icons in the text by leveraging the syntax:
*
* `My text $(icon-name) contains icons like $(icon-name) this one.`
*
* Where the icon-name is taken from the [octicon](https://octicons.github.com) icon set, e.g.
* `light-bulb`, `thumbsup`, `zap` etc.
*/
text: string

/**
* Shows the entry in the status bar.
*/
show(): void

/**
* Hide the entry in the status bar.
*/
hide(): void

/**
* Dispose and free associated resources. Call
* [hide](#StatusBarItem.hide).
*/
dispose(): void
}

export interface Env {
completeOpt: string
readonly isVim: boolean
Expand Down Expand Up @@ -716,7 +756,7 @@ export interface IWorkspace {
createFileSystemWatcher(globPattern: string, ignoreCreate?: boolean, ignoreChange?: boolean, ignoreDelete?: boolean): FileSystemWatcher
getConfiguration(section?: string, _resource?: string): WorkspaceConfiguration
registerTextDocumentContentProvider(scheme: string, provider: TextDocumentContentProvider): Disposable
getQuickfixItem(loc: Location): Promise<QuickfixItem>
getQuickfixItem(loc: Location, text?: string, type?: string): Promise<QuickfixItem>
getLine(uri: string, line: number): Promise<string>
readFile(uri: string): Promise<string>
echoLines(lines: string[], truncate?: boolean): Promise<void>
Expand All @@ -734,6 +774,7 @@ export interface IWorkspace {
requestInput(title: string, defaultValue?: string): Promise<string>
match(selector: DocumentSelector, document: TextDocument): number
runCommand(cmd: string, cwd?: string, timeout?: number): Promise<string>
runTerminalCommand(cmd: string, cwd?: string): Promise<TerminalResult>
runTerminalCommand(cmd: string, cwd?: string, keepfocus?: boolean): Promise<TerminalResult>
createStatusBarItem(priority?: number): StatusBarItem
dispose(): void
}
18 changes: 13 additions & 5 deletions src/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ import Document from './model/document'
import FileSystemWatcher from './model/fileSystemWatcher'
import BufferChannel from './model/outputChannel'
import Terminal from './model/terminal'
import StatusLine from './model/status'
import WillSaveUntilHandler from './model/willSaveHandler'
import { ChangeInfo, ConfigurationChangeEvent, ConfigurationTarget, EditerState, Env, IConfigurationData, IConfigurationModel, IWorkspace, MsgTypes, OutputChannel, QuickfixItem, TerminalResult, TextDocumentWillSaveEvent, WorkspaceConfiguration } from './types'
import { ChangeInfo, ConfigurationChangeEvent, ConfigurationTarget, EditerState, Env, IConfigurationData, IConfigurationModel, IWorkspace, MsgTypes, OutputChannel, QuickfixItem, TerminalResult, TextDocumentWillSaveEvent, WorkspaceConfiguration, StatusBarItem, StatusItemOption } from './types'
import { mkdirAsync, renameAsync, resolveRoot, statAsync, writeFile, createTmpFile } from './util/fs'
import { disposeAll, echoErr, echoMessage, echoWarning, isSupportedScheme, runCommand, wait, watchFiles } from './util/index'
import { score } from './util/match'
Expand All @@ -34,6 +35,7 @@ export class Workspace implements IWorkspace {
public bufnr: number

private willSaveUntilHandler: WillSaveUntilHandler
private statusLine: StatusLine
private _env: Env
private _cwd = process.cwd()
private _root = process.cwd()
Expand Down Expand Up @@ -81,6 +83,7 @@ export class Workspace implements IWorkspace {

public async init(): Promise<void> {
this.terminal = new Terminal(this.nvim)
this.statusLine = new StatusLine(this.nvim)
events.on('BufEnter', bufnr => {
this.bufnr = bufnr
}, null, this.disposables)
Expand Down Expand Up @@ -350,7 +353,7 @@ export class Workspace implements IWorkspace {
return true
}

public async getQuickfixItem(loc: Location, text?: string): Promise<QuickfixItem> {
public async getQuickfixItem(loc: Location, text?: string, type = ''): Promise<QuickfixItem> {
let { cwd, nvim } = this
let { uri, range } = loc
let { line, character } = range.start
Expand All @@ -364,6 +367,7 @@ export class Workspace implements IWorkspace {
col: character + 1,
text
}
if (type) item.type = type
if (bufnr != -1) item.bufnr = bufnr
return item
}
Expand Down Expand Up @@ -637,9 +641,8 @@ export class Workspace implements IWorkspace {
return runCommand(cmd, cwd, timeout)
}

public async runTerminalCommand(cmd: string, cwd?: string): Promise<TerminalResult> {
cwd = cwd || this.root
return await this.terminal.runCommand(cmd, cwd)
public async runTerminalCommand(cmd: string, cwd = this.cwd, keepfocus = false): Promise<TerminalResult> {
return await this.terminal.runCommand(cmd, cwd, keepfocus)
}

public async showQuickpick(items: string[], placeholder = 'Choose by number'): Promise<number> {
Expand Down Expand Up @@ -705,6 +708,10 @@ export class Workspace implements IWorkspace {
})
}

public createStatusBarItem(priority = 0, opt: StatusItemOption = {}): StatusBarItem {
return this.statusLine.createStatusBarItem(priority, opt.progress || false)
}

private async setupDocumentReadAutocmd(): Promise<void> {
let schemes = this.schemeProviderMap.keys()
let cmds: string[] = []
Expand Down Expand Up @@ -749,6 +756,7 @@ augroup end`
this.buffers.clear()
Watchman.dispose()
this.terminal.removeAllListeners()
this.statusLine.dispose()
disposeAll(this.disposables)
}

Expand Down

0 comments on commit 58ee4d6

Please sign in to comment.