Skip to content

Commit

Permalink
Refactor provider (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
melloware authored Aug 16, 2024
1 parent d04673e commit 4385ffc
Showing 1 changed file with 31 additions and 39 deletions.
70 changes: 31 additions & 39 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -324,63 +324,55 @@ function registerHoverProvider(languageSelector: string): Disposable {
* @returns {Hover | null} A Hover object containing the hover information, or null if no information is available.
*/
provideHover(document: TextDocument, position: Position): Hover | null {
//_output.appendLine(`In hover, position is ${position.line}:${position.character}`);
const wordRange: Range | undefined = document.getWordRangeAtPosition(position, /(\w+:\w+)|\w+[=]/);
if (!wordRange) {
//_output.appendLine('!wordRange');
return null;
}

let word = document.getText(wordRange);
//_output.appendLine(`Word: ${word}`);

const word = document.getText(wordRange);
aliasFromDocument(document, position);

let xmlnsPrefix: string,
componentName: string,
attributeName: string | null = null;

if (word.includes(':')) {
const xmlnsPrefix = '<' + word.split(':')[0] + ':';
const componentName = word.split(':')[1];
const xmlns = supportedXmlNamespaces.find((xmlns) => xmlns.aliasInDoc === xmlnsPrefix);
if (!xmlns) {
return null;
}
[xmlnsPrefix, componentName] = word.split(':');
xmlnsPrefix = '<' + xmlnsPrefix + ':';
} else {
attributeName = word.substring(0, word.length - 1);
const componentInfo: Map<string, string> = getComponentInformation(document, position);
xmlnsPrefix = componentInfo.get('xmlnsPrefix') ? '<' + componentInfo.get('xmlnsPrefix') + ':' : '';
componentName = componentInfo.get('componentName') || '';
}

const componentItem = xmlns.uniqueDefinitions.filter((definition) => definition.component.name === componentName).find(() => true);
if (!componentItem) {
return null;
}
const xmlns = supportedXmlNamespaces.find((xmlns) => xmlns.aliasInDoc === xmlnsPrefix);
if (!xmlns) {
return null;
}

const contents = new MarkdownString();
contents.appendMarkdown(`**${componentName}:** ${componentItem.component.description}`);
contents.isTrusted = true;
contents.supportHtml = true;
return new Hover(contents, new Range(position, position));
} else {
word = word.substring(0, word.length - 1);
const componentItem = xmlns.uniqueDefinitions.find((definition) => definition.component.name === componentName);
if (!componentItem) {
return null;
}

const componentInfo: Map<string, string> = getComponentInformation(document, position);
const xmlnsPrefix = componentInfo.get('xmlnsPrefix') ? '<' + componentInfo.get('xmlnsPrefix') + ':' : '';
const componentName = componentInfo.get('componentName');
const xmlns = supportedXmlNamespaces.find((xmlns) => xmlns.aliasInDoc === xmlnsPrefix);
if (!xmlns) {
return null;
}
const contents = new MarkdownString();
contents.isTrusted = true;
contents.supportHtml = true;

const componentItem = xmlns.uniqueDefinitions.filter((definition) => definition.component.name === componentName).find(() => true);
if (componentItem === undefined) {
if (attributeName) {
const attr = componentItem.component.attributes.find((attribute) => attribute.name === attributeName);
if (!attr) {
return null;
}

//_output.appendLine(`Found: ${xmlnsPrefix}, ${componentName}`);
const attr = componentItem.component.attributes.filter((attribute) => attribute.name == word)[0];

const contents = new MarkdownString();
contents.appendMarkdown(`**${attr.name}:** ${attr.description}\n\n`);
contents.appendMarkdown(`**Required:** ${attr.required}\n\n`);
contents.appendMarkdown(`**Type:** ${attr.type}\n\n`);
contents.isTrusted = true;
contents.supportHtml = true;
return new Hover(contents, new Range(position, position));
} else {
contents.appendMarkdown(`**${componentName}:** ${componentItem.component.description}`);
}

return new Hover(contents, new Range(position, position));
}
});
}
Expand Down

0 comments on commit 4385ffc

Please sign in to comment.