Skip to content

Commit

Permalink
Refactor HoverProviders to confirm to the new structure
Browse files Browse the repository at this point in the history
  • Loading branch information
Shresht7 committed Jan 1, 2024
1 parent 2941078 commit 37ec583
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 56 deletions.
41 changes: 19 additions & 22 deletions src/providers/hover/localization.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
// Library
import * as vscode from 'vscode';
import { HoverProvider } from './_base';

// ---------------------------
// LOCALIZATION HOVER PROVIDER
// ---------------------------

/** Provides hover information for localization handles in xml files */
export class LocalizationHoverProvider {
export class LocalizationHoverProvider extends HoverProvider {

/** Regular expression to match localization handles
* @example h55e8dd17gb5c8g4d34gac23gd8616a2c1925
Expand All @@ -17,9 +18,12 @@ export class LocalizationHoverProvider {
private static references: Map<string, string> = new Map();

/** A selector that defines the documents this provider is applicable to */
private static documentSelector = ['xml'];
private static documentSelector: vscode.DocumentSelector = ['xml'];

private constructor() { }
constructor() {
super(LocalizationHoverProvider.documentSelector);
this.register();
}

/** Lookup the localization handle in the references map and returns the corresponding contents
* @param handle The localization handle to lookup
Expand All @@ -41,29 +45,22 @@ export class LocalizationHoverProvider {
this.references.clear();
}

/**
* Register this hover provider to vscode
* @returns A vscode.Disposable that calls a `dispose()` function to release resources when disposed
*/
static register(): vscode.Disposable {
return vscode.languages.registerHoverProvider(this.documentSelector, {
provideHover(document: vscode.TextDocument, position: vscode.Position, token: vscode.CancellationToken): vscode.ProviderResult<vscode.Hover> {
override provideHover(document: vscode.TextDocument, position: vscode.Position, token: vscode.CancellationToken): vscode.ProviderResult<vscode.Hover> {

// Get the word that is currently being hovered over
const range = document.getWordRangeAtPosition(position);
const word = document.getText(range);
// Get the word that is currently being hovered over
const range = document.getWordRangeAtPosition(position);
const word = document.getText(range);

// Return early if the word is not a localization handle
if (!LocalizationHoverProvider.handleRegex.test(word)) { return; }
// Return early if the word is not a localization handle
if (!LocalizationHoverProvider.handleRegex.test(word)) { return; }

// Get content for the given localization handle
const content = LocalizationHoverProvider.getContent(word);
if (!content) { return; }
// Get content for the given localization handle
const content = LocalizationHoverProvider.getContent(word);
if (!content) { return; }

// Return the content to be shown on hover
return new vscode.Hover(content);
// Return the content to be shown on hover
return new vscode.Hover(content);

}
});
}

}
60 changes: 28 additions & 32 deletions src/providers/hover/version.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Library
import * as vscode from 'vscode';
import { bg3 } from '../../library';
import { HoverProvider } from './_base';

// ----------------------
// VERSION HOVER PROVIDER
Expand All @@ -10,42 +11,37 @@ import { bg3 } from '../../library';
* Shows the string representation of the version number
* when hovering over the int64 version number in the `meta.lsx` file
*/
export class VersionHoverProvider {
export class VersionHoverProvider extends HoverProvider {

/** A selector that defines the documents this provider is applicable to */
private static documentSelector = ['xml'];

private constructor() { }

/**
* Register this hover provider to vscode
* @returns A vscode.Disposable that calls a `dispose()` function to release resources when disposed
*/
static register(): vscode.Disposable {
return vscode.languages.registerHoverProvider(this.documentSelector, {
provideHover(document, position, token) {

// Get the line that is currently being hovered over
const line = document.lineAt(position.line);
// Check if the line matches the version number regex
if (!bg3.Version.lsxRegex.test(line.text)) { return; };

// Capture the int64 version number from the line and parse as BigInt
const capture = bg3.Version.lsxRegex.exec(line.text)?.at(1) || '0';
const bigIntVersion = BigInt(capture);

// Get the word that is being currently hovered over
const range = document.getWordRangeAtPosition(position);
const word = document.getText(range);

// If hovering over the version number, show its string representation
if (word === capture) {
const version = new bg3.Version(bigIntVersion);
return new vscode.Hover(version.toString());
}

}
});
constructor() {
super(VersionHoverProvider.documentSelector);
this.register();
}

override provideHover(document: vscode.TextDocument, position: vscode.Position, token: vscode.CancellationToken): vscode.ProviderResult<vscode.Hover> {

// Get the line that is currently being hovered over
const line = document.lineAt(position.line);
// Check if the line matches the version number regex
if (!bg3.Version.lsxRegex.test(line.text)) { return; };

// Capture the int64 version number from the line and parse as BigInt
const capture = bg3.Version.lsxRegex.exec(line.text)?.at(1) || '0';
const bigIntVersion = BigInt(capture);

// Get the word that is being currently hovered over
const range = document.getWordRangeAtPosition(position);
const word = document.getText(range);

// If hovering over the version number, show its string representation
if (word === capture) {
const version = new bg3.Version(bigIntVersion);
return new vscode.Hover(version.toString());
}

}

};
4 changes: 2 additions & 2 deletions src/providers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ import { VersionHoverProvider, LocalizationHoverProvider } from './hover';

/** An array of disposables for the registered providers */
export const providers: vscode.Disposable[] = [
VersionHoverProvider.register(),
LocalizationHoverProvider.register(),
new VersionHoverProvider(),
new LocalizationHoverProvider(),
];

0 comments on commit 37ec583

Please sign in to comment.