From e5d6b0bd3edf9371bd99c2deafaa38f9c05093fc Mon Sep 17 00:00:00 2001 From: Callum Tolley Date: Wed, 9 Mar 2022 11:08:07 +0000 Subject: [PATCH 1/6] Rework logic for name/details creation for document symbols --- src/features/documentSymbolProvider.ts | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/features/documentSymbolProvider.ts b/src/features/documentSymbolProvider.ts index 6f010495c..7d47d1857 100644 --- a/src/features/documentSymbolProvider.ts +++ b/src/features/documentSymbolProvider.ts @@ -50,7 +50,21 @@ function createSymbolForElement(element: Structure.CodeElement): vscode.Document const fullRange = element.Ranges[SymbolRangeNames.Full]; const nameRange = element.Ranges[SymbolRangeNames.Name]; - return new vscode.DocumentSymbol(element.DisplayName, /*detail*/ "", toSymbolKind(element.Kind), toRange3(fullRange), toRange3(nameRange)); + var name = element.DisplayName; + switch (element.Kind) { + case SymbolKinds.Class: + case SymbolKinds.Delegate: + case SymbolKinds.Enum: + case SymbolKinds.Interface: + case SymbolKinds.Struct: + name = element.Name; + break; + default: + break; + } + const details = name == element.DisplayName ? "" : element.DisplayName; + + return new vscode.DocumentSymbol(name, details, toSymbolKind(element.Kind), toRange3(fullRange), toRange3(nameRange)); } const kinds: { [kind: string]: vscode.SymbolKind; } = {}; From d991996555e5e6ab4f44730b098b074cffa4c467 Mon Sep 17 00:00:00 2001 From: Callum Tolley Date: Mon, 16 Jan 2023 00:34:01 +0000 Subject: [PATCH 2/6] Change logic --- src/features/documentSymbolProvider.ts | 47 +++++++++++++++++++------- 1 file changed, 35 insertions(+), 12 deletions(-) diff --git a/src/features/documentSymbolProvider.ts b/src/features/documentSymbolProvider.ts index 7d47d1857..03d8222c1 100644 --- a/src/features/documentSymbolProvider.ts +++ b/src/features/documentSymbolProvider.ts @@ -31,13 +31,13 @@ export default class OmnisharpDocumentSymbolProvider extends AbstractSupport imp } } -function createSymbols(elements: Structure.CodeElement[]): vscode.DocumentSymbol[] { +function createSymbols(elements: Structure.CodeElement[], parentElement?: Structure.CodeElement): vscode.DocumentSymbol[] { let results: vscode.DocumentSymbol[] = []; elements.forEach(element => { - let symbol = createSymbolForElement(element); + let symbol = createSymbolForElement(element, parentElement); if (element.Children) { - symbol.children = createSymbols(element.Children); + symbol.children = createSymbols(element.Children, element); } results.push(symbol); @@ -46,23 +46,46 @@ function createSymbols(elements: Structure.CodeElement[]): vscode.DocumentSymbol return results; } -function createSymbolForElement(element: Structure.CodeElement): vscode.DocumentSymbol { - const fullRange = element.Ranges[SymbolRangeNames.Full]; - const nameRange = element.Ranges[SymbolRangeNames.Name]; - - var name = element.DisplayName; +function getNameForElement(element: Structure.CodeElement, parentElement?: Structure.CodeElement): string { switch (element.Kind) { case SymbolKinds.Class: case SymbolKinds.Delegate: case SymbolKinds.Enum: case SymbolKinds.Interface: case SymbolKinds.Struct: - name = element.Name; - break; + case SymbolKinds.Namespace: + if (typeof parentElement === 'undefined') + return element.DisplayName; + const prefix = `${parentElement.DisplayName}.`; + return element.DisplayName.startsWith(prefix) ? element.DisplayName.slice(prefix.length) : element.DisplayName; + + case SymbolKinds.Constant: + case SymbolKinds.Constructor: + case SymbolKinds.Destructor: + case SymbolKinds.EnumMember: + case SymbolKinds.Event: + case SymbolKinds.Field: + case SymbolKinds.Indexer: + case SymbolKinds.Method: + case SymbolKinds.Operator: + case SymbolKinds.Property: + case SymbolKinds.Unknown: default: - break; + return element.Name; } - const details = name == element.DisplayName ? "" : element.DisplayName; +} + +function getNameAndDetailsForElement(element: Structure.CodeElement, parentElement?: Structure.CodeElement): { name: string, details: string } { + const name = getNameForElement(element, parentElement); + const details = element.DisplayName === name ? '' : element.DisplayName; + + return { name, details }; +} + +function createSymbolForElement(element: Structure.CodeElement, parentElement?: Structure.CodeElement): vscode.DocumentSymbol { + const fullRange = element.Ranges[SymbolRangeNames.Full]; + const nameRange = element.Ranges[SymbolRangeNames.Name]; + const { name, details } = getNameAndDetailsForElement(element, parentElement); return new vscode.DocumentSymbol(name, details, toSymbolKind(element.Kind), toRange3(fullRange), toRange3(nameRange)); } From 7954c8df0209078e4bcce761d2adac20c37240f6 Mon Sep 17 00:00:00 2001 From: Callum Tolley Date: Mon, 16 Jan 2023 00:37:04 +0000 Subject: [PATCH 3/6] Brace --- src/features/documentSymbolProvider.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/features/documentSymbolProvider.ts b/src/features/documentSymbolProvider.ts index 03d8222c1..55840e4c2 100644 --- a/src/features/documentSymbolProvider.ts +++ b/src/features/documentSymbolProvider.ts @@ -54,8 +54,9 @@ function getNameForElement(element: Structure.CodeElement, parentElement?: Struc case SymbolKinds.Interface: case SymbolKinds.Struct: case SymbolKinds.Namespace: - if (typeof parentElement === 'undefined') + if (typeof parentElement === 'undefined') { return element.DisplayName; + } const prefix = `${parentElement.DisplayName}.`; return element.DisplayName.startsWith(prefix) ? element.DisplayName.slice(prefix.length) : element.DisplayName; From ae1091ece5ccfb2a1210fd5b536ff92ca26f163f Mon Sep 17 00:00:00 2001 From: Callum Tolley Date: Mon, 16 Jan 2023 01:06:30 +0000 Subject: [PATCH 4/6] Only make changes for objects --- .vscode/launch.json | 2 +- .vscode/tasks.json | 7 ++++++- src/features/documentSymbolProvider.ts | 22 +++++++++++----------- 3 files changed, 18 insertions(+), 13 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index c3e74961a..a59715396 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -188,4 +188,4 @@ "cwd": "${workspaceFolder}" } ] -} \ No newline at end of file +} diff --git a/.vscode/tasks.json b/.vscode/tasks.json index abacc0517..25c89d9e8 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -31,6 +31,11 @@ "group": { "kind": "build", "isDefault": true + }, + "options": { + "env": { + "NODE_OPTIONS": "--openssl-legacy-provider" + }, } }, { @@ -68,4 +73,4 @@ } } ] -} \ No newline at end of file +} diff --git a/src/features/documentSymbolProvider.ts b/src/features/documentSymbolProvider.ts index 55840e4c2..a65dde79d 100644 --- a/src/features/documentSymbolProvider.ts +++ b/src/features/documentSymbolProvider.ts @@ -46,7 +46,7 @@ function createSymbols(elements: Structure.CodeElement[], parentElement?: Struct return results; } -function getNameForElement(element: Structure.CodeElement, parentElement?: Structure.CodeElement): string { +function getNameAndDetailsForElement(element: Structure.CodeElement, parentElement?: Structure.CodeElement): { name: string, details: string } { switch (element.Kind) { case SymbolKinds.Class: case SymbolKinds.Delegate: @@ -55,10 +55,17 @@ function getNameForElement(element: Structure.CodeElement, parentElement?: Struc case SymbolKinds.Struct: case SymbolKinds.Namespace: if (typeof parentElement === 'undefined') { - return element.DisplayName; + return { name: element.DisplayName, details: '' }; } const prefix = `${parentElement.DisplayName}.`; - return element.DisplayName.startsWith(prefix) ? element.DisplayName.slice(prefix.length) : element.DisplayName; + if (!element.DisplayName.startsWith(prefix)) { + return { name: element.DisplayName, details: '' }; + } + const name = element.DisplayName.slice(prefix.length); + if (name === element.DisplayName) { + return { name: element.DisplayName, details: '' }; + } + return { name, details: element.DisplayName }; case SymbolKinds.Constant: case SymbolKinds.Constructor: @@ -72,17 +79,10 @@ function getNameForElement(element: Structure.CodeElement, parentElement?: Struc case SymbolKinds.Property: case SymbolKinds.Unknown: default: - return element.Name; + return { name: element.DisplayName, details: '' }; } } -function getNameAndDetailsForElement(element: Structure.CodeElement, parentElement?: Structure.CodeElement): { name: string, details: string } { - const name = getNameForElement(element, parentElement); - const details = element.DisplayName === name ? '' : element.DisplayName; - - return { name, details }; -} - function createSymbolForElement(element: Structure.CodeElement, parentElement?: Structure.CodeElement): vscode.DocumentSymbol { const fullRange = element.Ranges[SymbolRangeNames.Full]; const nameRange = element.Ranges[SymbolRangeNames.Name]; From 39fc7dd10c9b1f09a0cb39a9ad6bb683bf6eb453 Mon Sep 17 00:00:00 2001 From: Callum Tolley Date: Mon, 16 Jan 2023 01:06:44 +0000 Subject: [PATCH 5/6] Revert changes --- .vscode/launch.json | 2 +- .vscode/tasks.json | 7 +------ 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index a59715396..c3e74961a 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -188,4 +188,4 @@ "cwd": "${workspaceFolder}" } ] -} +} \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json index 25c89d9e8..abacc0517 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -31,11 +31,6 @@ "group": { "kind": "build", "isDefault": true - }, - "options": { - "env": { - "NODE_OPTIONS": "--openssl-legacy-provider" - }, } }, { @@ -73,4 +68,4 @@ } } ] -} +} \ No newline at end of file From 2cbebec934808e6c879a5f009c3b2a82c7475b75 Mon Sep 17 00:00:00 2001 From: Callum Tolley Date: Fri, 3 Feb 2023 19:55:07 +0000 Subject: [PATCH 6/6] Simplify code --- src/features/documentSymbolProvider.ts | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/src/features/documentSymbolProvider.ts b/src/features/documentSymbolProvider.ts index a65dde79d..3d8e88e14 100644 --- a/src/features/documentSymbolProvider.ts +++ b/src/features/documentSymbolProvider.ts @@ -46,26 +46,19 @@ function createSymbols(elements: Structure.CodeElement[], parentElement?: Struct return results; } -function getNameAndDetailsForElement(element: Structure.CodeElement, parentElement?: Structure.CodeElement): { name: string, details: string } { +function getNameForElement(element: Structure.CodeElement, parentElement?: Structure.CodeElement): string { switch (element.Kind) { case SymbolKinds.Class: case SymbolKinds.Delegate: case SymbolKinds.Enum: case SymbolKinds.Interface: case SymbolKinds.Struct: + return element.Name; + case SymbolKinds.Namespace: - if (typeof parentElement === 'undefined') { - return { name: element.DisplayName, details: '' }; - } - const prefix = `${parentElement.DisplayName}.`; - if (!element.DisplayName.startsWith(prefix)) { - return { name: element.DisplayName, details: '' }; - } - const name = element.DisplayName.slice(prefix.length); - if (name === element.DisplayName) { - return { name: element.DisplayName, details: '' }; - } - return { name, details: element.DisplayName }; + return typeof parentElement !== 'undefined' && element.DisplayName.startsWith(`${parentElement.DisplayName}.`) + ? element.DisplayName.slice(parentElement.DisplayName.length + 1) + : element.DisplayName; case SymbolKinds.Constant: case SymbolKinds.Constructor: @@ -79,14 +72,15 @@ function getNameAndDetailsForElement(element: Structure.CodeElement, parentEleme case SymbolKinds.Property: case SymbolKinds.Unknown: default: - return { name: element.DisplayName, details: '' }; + return element.DisplayName; } } function createSymbolForElement(element: Structure.CodeElement, parentElement?: Structure.CodeElement): vscode.DocumentSymbol { const fullRange = element.Ranges[SymbolRangeNames.Full]; const nameRange = element.Ranges[SymbolRangeNames.Name]; - const { name, details } = getNameAndDetailsForElement(element, parentElement); + const name = getNameForElement(element, parentElement); + const details = name === element.DisplayName ? '' : element.DisplayName; return new vscode.DocumentSymbol(name, details, toSymbolKind(element.Kind), toRange3(fullRange), toRange3(nameRange)); }