Skip to content

Commit

Permalink
fix(highlight): Add concealable flag and force render
Browse files Browse the repository at this point in the history
  • Loading branch information
weirongxu committed Dec 24, 2019
1 parent f74d17e commit 04f3384
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 38 deletions.
2 changes: 1 addition & 1 deletion src/explorer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,7 @@ export class Explorer {

// await this.clearContent();
for (const source of this.sources) {
await source.render({ notify: true, storeCursor: false });
await source.render({ notify: true, storeCursor: false, force: true });
}

if (store) {
Expand Down
94 changes: 64 additions & 30 deletions src/source/highlight-manager.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { workspace } from 'coc.nvim';
import { execNotifyBlock, skipOnBufEnter } from '../util';
import { execNotifyBlock, skipOnBufEnter, enableDebug } from '../util';

export const HlEscapeCode = {
left: (s: string | number) => HlEscapeCode.leftBegin + s + HlEscapeCode.leftEnd,
Expand Down Expand Up @@ -27,9 +27,16 @@ class HighlightManager {

nvim = workspace.nvim;
highlights: Hightlight[] = [];
concealableHighlightsVisible: Map<string, boolean> = new Map();
concealableHighlights: HighlightConcealable[] = [];

private requestedConcealableToggle: Map<string, (notify: boolean) => Promise<void>> = new Map();
private requestedConcealableQueue: Map<
string,
{
handler: (notify: boolean) => Promise<void>;
action: 'show' | 'hide';
}
> = new Map();

createMarkerID() {
HighlightManager.maxMarkerID += 1;
Expand Down Expand Up @@ -61,40 +68,67 @@ class HighlightManager {

const concealable: HighlightConcealable = {
markerID,
requestHide: () => this.requestedConcealableToggle.set(group, hide),
requestShow: () => this.requestedConcealableToggle.set(group, show),
requestShow: () =>
this.requestedConcealableQueue.set(group, { action: 'show', handler: show }),
requestHide: () =>
this.requestedConcealableQueue.set(group, { action: 'hide', handler: hide }),
};
this.concealableHighlights.push(concealable);
return concealable;
}

async emitRequestConcealableToggle(explorer: Explorer, notify = false) {
if (this.requestedConcealableToggle.size > 0) {
const { nvim } = this;
const { mode } = await nvim.mode;
const winnr = await explorer.winnr;
if (winnr && mode === 'n') {
await execNotifyBlock(async () => {
const storeWinnr = await nvim.call('winnr');
const jumpWin = storeWinnr !== winnr;
if (jumpWin) {
skipOnBufEnter(
(await nvim.eval(`[winbufnr(${winnr}), winbufnr(${storeWinnr})]`)) as [
number,
number,
],
);
nvim.command(`${winnr}wincmd w`, true);
}
for (const toggle of this.requestedConcealableToggle.values()) {
await toggle(true);
}
if (jumpWin) {
nvim.command(`${storeWinnr}wincmd w`, true);
}
this.requestedConcealableToggle.clear();
}, notify);
async emitRequestedConcealableRender(explorer: Explorer, { notify = false, force = false } = {}) {
if (this.requestedConcealableQueue.size <= 0) {
return;
}

const { nvim } = this;
const { mode } = await nvim.mode;
const winnr = await explorer.winnr;

const storeWinnr = await nvim.call('winnr');
const jumpWin = storeWinnr !== winnr;

const requestedRenderList = Array.from(this.requestedConcealableQueue).filter(
([group, value]) => {
if (force || !this.concealableHighlightsVisible.has(group)) {
return true;
}
const visible = this.concealableHighlightsVisible.get(group)!;
return (value.action === 'show' && !visible) || (value.action === 'hide' && visible);
},
);
this.requestedConcealableQueue.clear();

if (!requestedRenderList.length) {
return;
}

if (winnr && mode === 'n') {
if (enableDebug) {
// tslint:disable-next-line: ban
workspace.showMessage(
`Concealable Render ${requestedRenderList.length} (${Array.from(
requestedRenderList.map(([group]) => group),
).join(',')})`,
'more',
);
}
await execNotifyBlock(async () => {
if (jumpWin) {
skipOnBufEnter(
(await nvim.eval(`[winbufnr(${winnr}), winbufnr(${storeWinnr})]`)) as [number, number],
);
nvim.command(`${winnr}wincmd w`, true);
}
for (const [group, value] of requestedRenderList) {
this.concealableHighlightsVisible.set(group, value.action === 'show');
await value.handler(true);
}
if (jumpWin) {
nvim.command(`${storeWinnr}wincmd w`, true);
}
}, notify);
}
}

Expand Down
14 changes: 7 additions & 7 deletions src/source/source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export abstract class ExplorerSource<TreeNode extends BaseTreeNode<TreeNode>> {
this.addAction(
'refresh',
async () => {
await this.reload(this.rootNode);
await this.reload(this.rootNode, { force: true });
await this.explorer.executeHighlightSyntax();
},
'refresh',
Expand Down Expand Up @@ -446,9 +446,9 @@ export abstract class ExplorerSource<TreeNode extends BaseTreeNode<TreeNode>> {
/**
* @returns return true to redraw all rows
*/
async beforeDraw(nodes: TreeNode[]) {
async beforeDraw(nodes: TreeNode[], { force = false } = {}) {
const renderAll = this.columnManager.beforeDraw(nodes);
await hlGroupManager.emitRequestConcealableToggle(this.explorer);
await hlGroupManager.emitRequestedConcealableRender(this.explorer, { force });
return renderAll;
}

Expand Down Expand Up @@ -502,12 +502,12 @@ export abstract class ExplorerSource<TreeNode extends BaseTreeNode<TreeNode>> {

opened(_notify = false): void | Promise<void> {}

async reload(node: TreeNode, { render = true, notify = false } = {}) {
async reload(node: TreeNode, { render = true, notify = false, force = false } = {}) {
this.selectedNodes = new Set();
node.children = this.expandStore.isExpanded(node) ? await this.loadChildren(node) : [];
await this.loaded(node);
if (render) {
await this.render({ node, notify });
await this.render({ node, notify, force });
}
}

Expand Down Expand Up @@ -675,7 +675,7 @@ export abstract class ExplorerSource<TreeNode extends BaseTreeNode<TreeNode>> {
}, notify);
}

async render({ node = this.rootNode, notify = false, storeCursor = true } = {}) {
async render({ node = this.rootNode, notify = false, storeCursor = true, force = false } = {}) {
if (this.explorer.isHelpUI) {
return;
}
Expand Down Expand Up @@ -711,7 +711,7 @@ export abstract class ExplorerSource<TreeNode extends BaseTreeNode<TreeNode>> {
);
}
this.offsetAfterLine(newHeight - oldHeight, this.endLine);
await this.beforeDraw(this.flattenedNodes);
await this.beforeDraw(this.flattenedNodes, { force });
await this.drawNodes(this.flattenedNodes);

const sourceIndex = this.currentSourceIndex();
Expand Down

0 comments on commit 04f3384

Please sign in to comment.