Skip to content

Commit

Permalink
feat(api): add vscode.get_status_item (#1576)
Browse files Browse the repository at this point in the history
  • Loading branch information
xiyaowong committed Oct 29, 2023
1 parent f10cb1a commit c20ce2f
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 62 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ Load module: `local vscode = require("vscode-neovim")`
6. `vscode.update_config` update a configuration
7. `vscode.notify` like `vim.notify`, but use vscode notification to show the message
8. `vscode.to_op` A helper for `map-operator`. See [code_actions.lua](./runtime/plugin/code_actions.lua) for the usage
9. `vscode.get_status_item` Creates a status item

#### Actions

Expand Down Expand Up @@ -510,6 +511,20 @@ You can set `vscode.notify` as your default notify functions.
vim.notify = vscode.notify
```

##### `vscode.get_status_item(id)`

Creates a status item

- `id` (string): The identifier of the item

```lua
local test = vscode.get_status_item('test')
test.text = 'hello' -- Show the text
test.text = '' -- Hide the item
test.text = nil -- Close the item
test.text = '' -- error: The status item "test" has been closed
```

## ⌨️ Bindings

These are the default commands and bindings available for file/scroll/window/tab management.
Expand Down
2 changes: 2 additions & 0 deletions runtime/lua/vscode-neovim.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ local vscode = {
notify = api.notify,
-- operatorfunc helper
to_op = api.to_op,
-- status item
get_status_item = api.get_status_item,
}

return vscode
64 changes: 64 additions & 0 deletions runtime/lua/vscode-neovim/api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,10 @@ function M.notify(msg, level, opts)
M.action("notify", { args = { msg, level_name } })
end

---------------------------------
------ map-operator helper ------
---------------------------------

do
---@class Context
---@field range lsp.Range
Expand Down Expand Up @@ -361,4 +365,64 @@ do
end
end

--------------------------
------ status item ------
--------------------------

do
local _items = {}
local status_items = setmetatable({}, {
__newindex = function(_, k, v)
_items[k] = v
local status = vim.tbl_values(_items)
status = vim.tbl_filter(function(i)
return i ~= ""
end, status)
M.action("refresh-status-items", { args = { status } })
end,
__index = function(_, k)
return _items[k]
end,
})

---Creates a status item
---
---```lua
---local test = vscode.get_status_item('test')
---test.text = 'hello' -- Show the text
---test.text = '' -- Hide the item
---test.text = nil -- Close the item
---test.text = '' -- error: The status item "test" has been closed
---```
---
---@param id string The identifier of the item
function M.get_status_item(id)
vim.validate({ id = { id, "s" } })
status_items[id] = ""
return setmetatable({}, {
__newindex = function(_, k, v)
if k == "text" then
local curr = status_items[id]
if not curr then
error(([[The status item "%s" has been closed]]):format(id))
end
vim.validate({ text = { v, "s", true } })
if curr ~= v then
status_items[id] = v
end
end
end,
__index = function(_, k)
if k == "text" then
local curr = status_items[id]
if not curr then
error(([[The status item "%s" has been closed]]):format(id))
end
return curr
end
end,
})
end
end

return M
46 changes: 0 additions & 46 deletions src/status_line.ts

This file was deleted.

70 changes: 54 additions & 16 deletions src/status_line_manager.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,63 @@
import { Disposable } from "vscode";
import { Disposable, StatusBarAlignment, StatusBarItem, window } from "vscode";

import { MainController } from "./main_controller";
import { StatusLineController } from "./status_line";
import actions from "./actions";
import { config } from "./config";
import { EventBusData, eventBus } from "./eventBus";
import { MainController } from "./main_controller";
import { disposeAll } from "./utils";

enum StatusType {
Mode, // msg_showmode
Cmd, // msg_showcmd
Msg, // msg_show, msg_clear
Custom, // custom status item
}

export class StatusLineManager implements Disposable {
private disposables: Disposable[] = [];
/**
* Status var UI
*/
private statusLine: StatusLineController;

// ui events
private _modeText = "";
private _cmdText = "";
private _msgText = "";
// custom status items
private _customStatus: string[] = [];

private statusBar: StatusBarItem;

private get client() {
return this.main.client;
}

public constructor(private main: MainController) {
this.statusLine = new StatusLineController();
this.disposables.push(this.statusLine);
eventBus.on("redraw", this.handleRedraw, this, this.disposables);
this.statusBar = window.createStatusBarItem(StatusBarAlignment.Left, -10);
this.statusBar.show();
this.disposables.push(this.statusBar, eventBus.on("redraw", this.handleRedraw, this));
actions.add("refresh-status-items", (items: string[]) => this.setStatus(items, StatusType.Custom));
}

public dispose(): void {
disposeAll(this.disposables);
private setStatus(status: string[], type: StatusType.Custom): void;
private setStatus(status: string, type: Omit<StatusType, "Custom">): void;
private setStatus(status: string | string[], type: StatusType): void {
if (Array.isArray(status)) {
this._customStatus = status;
} else {
switch (type) {
case StatusType.Mode:
this._modeText = status;
break;
case StatusType.Cmd:
this._cmdText = status;
break;
case StatusType.Msg:
this._msgText = status;
break;
}
}
this.statusBar.text = [this._modeText, this._cmdText, this._msgText, ...this._customStatus]
.map((i) => i.replace(/\n/g, " ").trim())
.filter((i) => i.length)
.join(config.statusLineSeparator);
}

private handleRedraw(data: EventBusData<"redraw">) {
Expand All @@ -45,7 +79,7 @@ export class StatusLineManager implements Disposable {
}
}
}
this.statusLine.statusString = str;
this.setStatus(str, StatusType.Cmd);
break;
}
case "msg_show": {
Expand All @@ -67,7 +101,7 @@ export class StatusLineManager implements Disposable {
}
}
}
this.statusLine.msgString = str;
this.setStatus(str, StatusType.Msg);
break;
}
case "msg_showmode": {
Expand All @@ -81,11 +115,11 @@ export class StatusLineManager implements Disposable {
}
}
}
this.statusLine.modeString = str;
this.setStatus(str, StatusType.Mode);
break;
}
case "msg_clear": {
this.statusLine.msgString = "";
this.setStatus("", StatusType.Msg);
break;
}
}
Expand All @@ -94,4 +128,8 @@ export class StatusLineManager implements Disposable {
this.client.input("<CR>");
}
}

dispose() {
disposeAll(this.disposables);
}
}

0 comments on commit c20ce2f

Please sign in to comment.