Skip to content

Commit

Permalink
[A] Provide outline sorting feature #54
Browse files Browse the repository at this point in the history
- Provide sorting by name、kind and position(default)
  • Loading branch information
Gerrnperl committed Mar 24, 2024
1 parent 65fc35e commit f654181
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 13 deletions.
6 changes: 6 additions & 0 deletions package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@
"om.cmd.unfreeze.title": "✓ Freeze",
"om.cmd.pin.title": "​  Pin",
"om.cmd.freeze.title": "​  Freeze",
"om.cmd.sortByPosition.title": "​  Sort by position",
"om.cmd.sortByName.title": "​  Sort by name",
"om.cmd.sortByKind.title": "​  Sort by kind",
"om.cmd.sortByPosition.titleChecked": "✓ Sort by position",
"om.cmd.sortByName.titleChecked": "✓ Sort by name",
"om.cmd.sortByKind.titleChecked": "✓ Sort by kind",
"om.cmd.toggleSearch.title": "Toggle Search",
"om.cmd.focusSearch.title": "Focus Search",
"om.cmd.workspace.closeFile.title": "Close File",
Expand Down
6 changes: 6 additions & 0 deletions package.nls.zh-cn.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@
"om.cmd.unfreeze.title": "✓ 冻结",
"om.cmd.pin.title": "​  固定",
"om.cmd.freeze.title": "​  冻结",
"om.cmd.sortByPosition.title": "​  按位置排序",
"om.cmd.sortByName.title": "​  按名称排序",
"om.cmd.sortByKind.title": "​  按类型排序",
"om.cmd.sortByPosition.titleChecked": "✓ 按位置排序",
"om.cmd.sortByName.titleChecked": "✓ 按名称排序",
"om.cmd.sortByKind.titleChecked": "✓ 按类型排序",
"om.cmd.toggleSearch.title": "切换搜索",
"om.cmd.focusSearch.title": "聚焦搜索",
"om.cmd.workspace.closeFile.title": "关闭文件",
Expand Down
27 changes: 22 additions & 5 deletions src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export class SymbolNode {
* @param parent The parent node of the tree.
* @returns The root node of the tree.
*/
static fromDocumentSymbols(docSymbols: DocumentSymbol[]) : SymbolNode[] {
static fromDocumentSymbols(docSymbols: DocumentSymbol[], sortBy: Sortby = Sortby.position) : SymbolNode[] {

const root: SymbolNode[] = [];
const hiddenItem = config.hiddenItem();
Expand Down Expand Up @@ -123,8 +123,19 @@ export class SymbolNode {
}

// Inner function to recursively transform the DocumentSymbols into SymbolNodes
function recursiveTransform(docSymbols: DocumentSymbol[], parent: SymbolNode | SymbolNode[]) {
docSymbols.sort((a, b) => a.range.start.line - b.range.start.line);
function recursiveTransform(docSymbols: DocumentSymbol[], parent: SymbolNode | SymbolNode[], sortBy: Sortby = Sortby.position) {
switch (sortBy) {
case Sortby.name:
docSymbols.sort((a,b) => a.name.localeCompare(b.name));
break;
case Sortby.kind:
docSymbols.sort((a,b) => a.kind.toString().localeCompare(b.kind.toString()));
break;
case Sortby.position:
default:
docSymbols.sort((a, b) => a.range.start.line - b.range.start.line);
break;
}
reconstructTree(docSymbols);
docSymbols.forEach((docSymbol) => {
const node = new SymbolNode(docSymbol);
Expand All @@ -140,12 +151,12 @@ export class SymbolNode {
parent.children.push(node);
}
if (docSymbol.children.length > 0) {
recursiveTransform(docSymbol.children, node);
recursiveTransform(docSymbol.children, node, sortBy);
}
});
}

recursiveTransform(docSymbols, root);
recursiveTransform(docSymbols, root, sortBy);

return root;
}
Expand Down Expand Up @@ -274,6 +285,12 @@ export enum PinStatus {
frozen,
}

export enum Sortby {
position,
name,
kind,
}

export interface PinSMsg {
type: 'pin';
data: {
Expand Down
48 changes: 47 additions & 1 deletion src/extension/commands.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Memento, Position, Uri, commands } from 'vscode';
import { OutlineView } from './outline';
import { config } from './config';
import { ChangeDepthMsg, FocusMsg, PinSMsg, PinStatus } from '../common';
import { ChangeDepthMsg, FocusMsg, PinSMsg, PinStatus, Sortby } from '../common';
import { SymbolTreeItem, WorkspaceSymbols } from './workspace';


Expand Down Expand Up @@ -70,6 +70,27 @@ function focusSearchField(outlineProvider: OutlineView, state: Memento) {
} as FocusMsg);
}

function setSortBy(outlineProvider: OutlineView, sortBy: Sortby, state: Memento) {
state.update('sortBy', sortBy);
commands.executeCommand('setContext', 'outline-map.sort-by', sortBy);
outlineProvider.sort(sortBy);
}

// export
function setSortByPosition(outlineProvider: OutlineView, state: Memento) {
setSortBy(outlineProvider, Sortby.position, state);
}

// export
function setSortByName(outlineProvider: OutlineView, state: Memento) {
setSortBy(outlineProvider, Sortby.name, state);
}

// export
function setSortByKind(outlineProvider: OutlineView, state: Memento) {
setSortBy(outlineProvider, Sortby.kind, state);
}

if (config.defaultMaxDepth() > 0) {
// if defaultMaxDepth is set, set the context to true
// so that the Add/Reduce Depth buttons are visible
Expand Down Expand Up @@ -112,6 +133,31 @@ export const OutlineViewCommandList: Command[] = [
name: 'outline-map.focusSearch',
fn: focusSearchField,
},
{
name: 'outline-map.sortByPosition',
fn: setSortByPosition,
},
{
name: 'outline-map.sortByName',
fn: setSortByName,
},
{
name: 'outline-map.sortByKind',
fn: setSortByKind,
},
{
name: 'outline-map.sortByPositionChecked',
fn: () => undefined,
},
{
name: 'outline-map.sortByNameChecked',
fn: () => undefined,
},
{
name: 'outline-map.sortByKindChecked',
fn: () => undefined,
},

];


Expand Down
12 changes: 10 additions & 2 deletions src/extension/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
import { OutlineView } from './outline';

import { OutlineViewCommandList, WorkspaceCommandList } from './commands';
import { FocusMsg, ScrollMsg } from '../common';
import { FocusMsg, PinStatus, ScrollMsg, Sortby } from '../common';
import { config } from './config';
import { debounce, throttle } from '../utils';
import { WorkspaceSymbols } from './workspace';
Expand All @@ -24,7 +24,7 @@ let workspaceSymbols = undefined as WorkspaceSymbols | undefined;
let regionSymbolProvider = undefined as RegionProvider | undefined;

export function activate(context: ExtensionContext) {

restoreContext(context);
if (config.workspaceEnabled()) {
initWorkspaceSymbols(context);
}
Expand Down Expand Up @@ -52,6 +52,14 @@ export function activate(context: ExtensionContext) {
);
}

/**
* Restore context from global state
* @param context
*/
function restoreContext(context: ExtensionContext) {
commands.executeCommand('setContext', 'outline-map.sort-by', context.globalState.get('sortBy', Sortby.position));
}

/**
* Initialize workspace symbols
* @param context
Expand Down
24 changes: 19 additions & 5 deletions src/extension/outline.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { config } from './config';
import { commands, DocumentSymbol, ExtensionContext, TextDocument, Uri, WebviewView, WebviewViewProvider, window, Range, Selection, Position, Diagnostic, DiagnosticSeverity, CancellationTokenSource } from 'vscode';
import { Msg, UpdateMsg, Op, UpdateOp, DeleteOp, InsertOp, SymbolNode, MoveOp, PinStatus, FocusMsg } from '../common';
import { Msg, UpdateMsg, Op, UpdateOp, DeleteOp, InsertOp, SymbolNode, MoveOp, PinStatus, FocusMsg, Sortby } from '../common';
import { WorkspaceSymbols } from './workspace';
import { RegionProvider } from './region';
import { deepClone } from '../utils';
Expand All @@ -13,6 +13,7 @@ interface OutlineViewInit {
workspaceSymbols?: WorkspaceSymbols;
regionProvider?: RegionProvider;
initialSearch?: boolean;
sortBy?: Sortby;
}

/**
Expand Down Expand Up @@ -44,17 +45,25 @@ export class OutlineView implements WebviewViewProvider {

private initialSearch: boolean;

private sortby: Sortby = Sortby.position;

constructor(init: OutlineViewInit) {
this.extensionUri = init.context.extensionUri;
this.workspaceSymbols = init.workspaceSymbols;
this.regionProvider = init.regionProvider;
this.initialSearch = init.initialSearch || false;
this.sortby = init.sortBy || Sortby.position;
}

pin(pinStatus: PinStatus) {
this.pinStatus = pinStatus;
}

sort(sortBy: Sortby) {
this.sortby = sortBy;
this.update(window.activeTextEditor?.document || window.visibleTextEditors[0].document);
}

resolveWebviewView(webviewView: WebviewView): void | Thenable<void> {
this.view = webviewView;

Expand Down Expand Up @@ -96,7 +105,9 @@ export class OutlineView implements WebviewViewProvider {
if (config.findRefEnabled()) {
res.then(() => {
commands.executeCommand(config.findRefUseFindImpl() ?
'references-view.findImplementations' : 'references-view.findReferences');
'references-view.findImplementations' : 'references-view.findReferences').then(ref=>{
config.debug() && console.log(ref);
});
});
}
}
Expand Down Expand Up @@ -405,7 +416,7 @@ export class OutlineView implements WebviewViewProvider {
if (!this.isValidDocument(textDocument)) { // Switched to a non-supported document like output
return;
}
const newOutlineTree = new OutlineTree(textDocument, this.regionProvider);
const newOutlineTree = new OutlineTree(textDocument, this.regionProvider, this.sortby);
newOutlineTree.updateSymbols().then(() => {
const newNodes = newOutlineTree.getNodes();
if (!newNodes || newNodes.length === 0) {
Expand Down Expand Up @@ -461,9 +472,12 @@ export class OutlineTree {
/** Times has tried to update the symbols. */
private attempts = 0;

constructor(textDocument: TextDocument, regionProvider?: RegionProvider) {
private sortBy: Sortby = Sortby.position;

constructor(textDocument: TextDocument, regionProvider?: RegionProvider, sortBy: Sortby = Sortby.position) {
this.textDocument = textDocument;
this.regionProvider = regionProvider;
this.sortBy = sortBy;
}

getNodes() {
Expand Down Expand Up @@ -496,7 +510,7 @@ export class OutlineTree {

if (docSymbols) {
config.debug() && console.log(`[Outline-map]: Got symbols from ${this.textDocument.uri.toString()}.`, docSymbols);
this.nodes = SymbolNode.fromDocumentSymbols(docSymbols);
this.nodes = SymbolNode.fromDocumentSymbols(docSymbols, this.sortBy);
// Reset attempts
this.attempts = 0;
}
Expand Down

0 comments on commit f654181

Please sign in to comment.