Skip to content

Commit efcad0e

Browse files
authored
Hover Provider (#202)
Hover Provider (#202)
1 parent 0692312 commit efcad0e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+1606
-447
lines changed

extension/src/commands.ts

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,33 @@
11
import * as vscode from "vscode";
22
import * as nls from 'vscode-nls';
3-
import { AutoLispExt } from './extension';
4-
import { openWebHelp } from './help/openWebHelp';
3+
import { AutoLispExt } from './context';
54
import { generateDocumentationSnippet, getDefunArguments, getDefunAtPosition } from './help/userDocumentation';
65
import { showErrorMessage } from './project/projectCommands';
76
import { AutoLispExtProvideDefinition } from './providers/gotoProvider';
87
import { AutoLispExtProvideReferences } from './providers/referenceProvider';
98
import { AutoLispExtPrepareRename, AutoLispExtProvideRenameEdits } from './providers/renameProvider';
109
import { SymbolManager } from './symbols';
10+
import {AutoLispExtProvideHover} from "./providers/hoverProvider";
11+
import { DocumentServices } from './services/documentServices';
1112

1213
const localize = nls.loadMessageBundle();
1314

1415
export function registerCommands(context: vscode.ExtensionContext){
1516

16-
// Associated with the right click "Open Online Help" menu item
1717
context.subscriptions.push(vscode.commands.registerCommand('autolisp.openWebHelp', async () => {
18+
// Note: this function is directly referenced by the package.json contributes (commands & menus) section.
19+
// Associated with the right click "Open Online Help" menu item.
1820
try {
19-
await openWebHelp();
21+
const editor: vscode.TextEditor = vscode.window.activeTextEditor;
22+
let selected: string = editor.document.getText(editor.selection);
23+
if (selected === "") {
24+
await vscode.commands.executeCommand('editor.action.addSelectionToNextFindMatch');
25+
selected = editor.document.getText(editor.selection);
26+
}
27+
let urlPath: string = AutoLispExt.WebHelpLibrary.getWebHelpUrlBySymbolName(selected, editor.document.fileName);
28+
if (urlPath.trim() !== ""){
29+
vscode.env.openExternal(vscode.Uri.parse(urlPath));
30+
}
2031
}
2132
catch (err) {
2233
if (err){
@@ -50,9 +61,12 @@ export function registerCommands(context: vscode.ExtensionContext){
5061
const vsDoc = vscode.window.activeTextEditor.document;
5162
const lf = vsDoc.eol === vscode.EndOfLine.LF ? '\n' : '\r\n';
5263
const doc = AutoLispExt.Documents.getDocument(vsDoc);
64+
if (!doc.isLSP) {
65+
return;
66+
}
5367

5468
// find the root LispContainer of the current cursor position
55-
const exp = doc.atomsForest.find(p => p.contains(pos));
69+
const exp = doc.documentContainer.atoms.find(p => p.contains(pos));
5670

5771
// Locate the Defun to decorate
5872
const def = await getDefunAtPosition(exp, pos);
@@ -73,7 +87,7 @@ export function registerCommands(context: vscode.ExtensionContext){
7387
}
7488
}));
7589

76-
AutoLispExt.Subscriptions.push(vscode.languages.registerDefinitionProvider([ 'autolisp', 'lisp'], {
90+
AutoLispExt.Subscriptions.push(vscode.languages.registerDefinitionProvider([DocumentServices.Selectors.LSP, 'lisp'], {
7791
provideDefinition: function (document: vscode.TextDocument, position: vscode.Position, token: vscode.CancellationToken)
7892
: vscode.ProviderResult<vscode.Definition | vscode.LocationLink[]> {
7993
// Purpose: locate potential source definitions of the underlying symbol
@@ -91,7 +105,7 @@ export function registerCommands(context: vscode.ExtensionContext){
91105
}));
92106

93107
const msgRenameFail = localize("autolispext.providers.rename.failed", "The symbol was invalid for renaming operations");
94-
AutoLispExt.Subscriptions.push(vscode.languages.registerRenameProvider(['autolisp', 'lisp'], {
108+
AutoLispExt.Subscriptions.push(vscode.languages.registerRenameProvider([DocumentServices.Selectors.LSP, 'lisp'], {
95109
prepareRename: function (document: vscode.TextDocument, position: vscode.Position, token: vscode.CancellationToken)
96110
: vscode.ProviderResult<vscode.Range | { range: vscode.Range; placeholder: string; }>
97111
{
@@ -133,7 +147,7 @@ export function registerCommands(context: vscode.ExtensionContext){
133147
}));
134148

135149

136-
AutoLispExt.Subscriptions.push(vscode.languages.registerReferenceProvider([ 'autolisp', 'lisp'], {
150+
AutoLispExt.Subscriptions.push(vscode.languages.registerReferenceProvider([ DocumentServices.Selectors.LSP, 'lisp'], {
137151
provideReferences: function (document: vscode.TextDocument, position: vscode.Position, context: vscode.ReferenceContext, token: vscode.CancellationToken)
138152
: vscode.ProviderResult<vscode.Location[]>
139153
{
@@ -152,4 +166,16 @@ export function registerCommands(context: vscode.ExtensionContext){
152166
}
153167
}));
154168

169+
AutoLispExt.Subscriptions.push(vscode.languages.registerHoverProvider([DocumentServices.Selectors.LSP, DocumentServices.Selectors.DCL], {
170+
provideHover(document: vscode.TextDocument, position: vscode.Position, token: vscode.CancellationToken): vscode.ProviderResult<vscode.Hover> {
171+
try {
172+
// offload all meaningful work to something that can be tested.
173+
const roDoc = AutoLispExt.Documents.getDocument(document);
174+
return AutoLispExtProvideHover(roDoc, position);
175+
} catch (err) {
176+
return; // No localized error since VSCode has a default "no results" response
177+
}
178+
}
179+
}));
180+
155181
}

extension/src/completion/autocompletionProvider.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import * as vscode from 'vscode';
22
import * as os from 'os';
33
import { isCursorInDoubleQuoteExpr } from "../format/autoIndent";
4-
import { allCmdsAndSysvars, internalDclKeys, internalLispFuncs, winOnlyListFuncPrefix } from "../resources";
4+
import { AutoLispExt} from "../context";
55

66

77
export function isInternalAutoLispOp(item: string): boolean {
88
if (!item)
99
return false;
1010

11-
for (let i = 0; i < internalLispFuncs.length; i++) {
12-
if (internalLispFuncs[i] === item)
11+
for (let i = 0; i < AutoLispExt.Resources.internalLispFuncs.length; i++) {
12+
if (AutoLispExt.Resources.internalLispFuncs[i] === item)
1313
return true;
1414
}
1515
return false;
@@ -117,9 +117,9 @@ export function getMatchingWord(document: vscode.TextDocument, position: vscode.
117117
export function getLispAndDclCompletions(document: vscode.TextDocument, word: string, isupper: boolean): vscode.CompletionItem[] {
118118
let currentLSPDoc = document.fileName;
119119
let ext = currentLSPDoc.substring(currentLSPDoc.length - 4, currentLSPDoc.length).toUpperCase();
120-
let candidatesItems = internalLispFuncs;
120+
let candidatesItems = AutoLispExt.Resources.internalLispFuncs;
121121
if (ext === ".DCL") {
122-
candidatesItems = internalDclKeys;
122+
candidatesItems = AutoLispExt.Resources.internalDclKeys;
123123
}
124124
let allSuggestions: Array<vscode.CompletionItem> = [];
125125
allSuggestions = getCompletionCandidates(candidatesItems, word, isupper);
@@ -129,7 +129,7 @@ export function getLispAndDclCompletions(document: vscode.TextDocument, word: st
129129
}
130130
else {
131131
return allSuggestions.filter(function(suggestion) {
132-
for (var prefix of winOnlyListFuncPrefix) {
132+
for (var prefix of AutoLispExt.Resources.winOnlyListFuncPrefix) {
133133
if (suggestion.label.toString().startsWith(prefix)) {
134134
return false;
135135
}
@@ -158,7 +158,7 @@ export function registerAutoCompletionProviders() {
158158

159159
var isInDoubleQuote = isCursorInDoubleQuoteExpr(document, position);
160160
if (isInDoubleQuote) {
161-
var cmds = getCmdAndVarsCompletionCandidates(allCmdsAndSysvars, inputword, userInputIsUpper);
161+
var cmds = getCmdAndVarsCompletionCandidates(AutoLispExt.Resources.allCmdsAndSysvars, inputword, userInputIsUpper);
162162
return cmds;
163163
}
164164

extension/src/context.ts

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,68 @@
11
import * as vscode from "vscode";
2-
import * as nls from "vscode-nls";
3-
import * as Resources from "./resources";
2+
import * as resources from "./resources";
43
import { Disposable } from 'vscode-languageclient';
54
import { DocumentManager } from './documents';
5+
import {DocumentServices} from "./services/documentServices";
6+
import { WebHelpLibrarySingleton } from './help/documentationLibrary';
67

8+
let _instance: ContextManager;
79

810
export class ContextManager{
911
private _ctx: vscode.ExtensionContext;
1012
private _docManager: DocumentManager;
11-
13+
private _init: boolean;
14+
private readonly _temp: Disposable[];
15+
16+
private constructor() {
17+
this._init = false;
18+
this._temp = [];
19+
}
20+
21+
static get Instance(): ContextManager {
22+
if (_instance) {
23+
return _instance;
24+
}
25+
return _instance = new ContextManager();
26+
}
1227

1328
get Context(): vscode.ExtensionContext {
1429
return this._ctx;
1530
}
1631
get Documents(): DocumentManager {
17-
return this._docManager;
32+
return this._docManager;
1833
}
19-
get Selectors(): typeof DocumentManager.Selectors {
20-
return DocumentManager.Selectors;
34+
get Selectors(): typeof DocumentServices.Selectors {
35+
return DocumentServices.Selectors;
2136
}
2237
get AllSelectors(): string[] {
23-
return [DocumentManager.Selectors.lsp, DocumentManager.Selectors.dcl, DocumentManager.Selectors.prj];
38+
return [DocumentServices.Selectors.LSP, DocumentServices.Selectors.DCL, DocumentServices.Selectors.PRJ];
2439
}
25-
get Subscriptions(): Disposable[] {
26-
return this._ctx ? this._ctx.subscriptions : null;
40+
get Subscriptions(): Disposable[] {
41+
if (this._temp.length > 0 && this._ctx){
42+
this._ctx.subscriptions.push(...this._temp);
43+
this._temp.length = 0;
44+
}
45+
return this._ctx ? this._ctx.subscriptions : this._temp;
2746
}
2847
get ExtPath(): string {
2948
return this._ctx ? this._ctx.extensionPath : "";
3049
}
3150
get Resources() {
32-
return Resources;
51+
return resources;
52+
}
53+
get WebHelpLibrary() {
54+
return WebHelpLibrarySingleton.Instance;
3355
}
3456

3557

3658
initialize(context: vscode.ExtensionContext): void {
37-
this._ctx = context;
38-
this._docManager = new DocumentManager();
59+
if (!this._init) {
60+
this._ctx = context;
61+
}
62+
this._docManager = DocumentManager.Instance;
63+
if (!resources.isLoaded)
64+
resources.loadAllResources();
3965
}
40-
}
66+
}
67+
68+
export const AutoLispExt: ContextManager = ContextManager.Instance;

0 commit comments

Comments
 (0)