Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add hover data to import urls #552

Merged
merged 1 commit into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 87 additions & 9 deletions src/server/hover/hover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,109 @@
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

import {Hover, HoverParams, MarkupKind} from 'vscode-languageserver';
import {
Hover,
HoverParams,
MarkupContent,
MarkupKind,
TextDocuments,
} from 'vscode-languageserver';
import {TextDocument} from 'vscode-languageserver-textdocument';
import {DocumentLocation, Model} from '@malloydata/malloy';

import {COMPLETION_DOCS} from '../../common/completion_docs';
import {parseWithCache} from '../parse_cache';
import {TranslateCache} from '../translate_cache';

export const getHover = (
// TODO: export from Malloy
type ImportLocation = Exclude<ReturnType<Model['getImport']>, undefined>;

export const getHover = async (
document: TextDocument,
documents: TextDocuments<TextDocument>,
translateCache: TranslateCache,
{position}: HoverParams
): Hover | null => {
): Promise<Hover | null> => {
const context = parseWithCache(document).helpContext(position);

if (context?.token) {
const name = context.token.replace(/:$/, '');

if (name) {
const value = COMPLETION_DOCS[context.type][name];
return {
contents: {
kind: MarkupKind.Markdown,
value,
},
};
if (value) {
return {
contents: {
kind: MarkupKind.Markdown,
value,
},
};
} else if (context.type === 'model_property') {
const model = await translateCache.translateWithCache(
document.uri,
document.version,
document.languageId
);
const importLocation = model?.getImport(position);
if (importLocation) {
return getImportHover(translateCache, documents, importLocation);
}

return null;
}
}
}

return null;
};

async function getImportHover(
translateCache: TranslateCache,
documents: TextDocuments<TextDocument>,
importLocation: ImportLocation
): Promise<Hover | null> {
const importedDocument = documents.get(importLocation.importURL) ?? {
uri: importLocation.importURL,
version: 0,
languageId: 'malloy',
};
const importedModel = await translateCache.translateWithCache(
importedDocument.uri,
importedDocument.version,
importedDocument.languageId
);
if (importedModel) {
const sources = bulletedList('Sources', importedModel.exportedExplores);
const queries = bulletedList('Queries', importedModel.namedQueries);
const markdown = `${sources}\n${queries}`;
const contents: MarkupContent = {
kind: MarkupKind.Markdown,
value: markdown,
};
return {
contents,
};
}
return null;
}

function bulletedList(
heading: string,
elements: {name: string; location?: DocumentLocation}[]
): string {
if (elements.length) {
return (
`* ${heading}\n` +
elements
.map(query => {
const name = query.name;
const uri = query.location
? `${query.location.url}#${query.location.range.start.line + 1}`
: '';
return ` * [${name}](${uri})`;
})
.join('\n')
);
}
return '';
}
4 changes: 2 additions & 2 deletions src/server/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,11 +256,11 @@ export const initServer = (
return resolveCompletionItem(item);
});

connection.onHover((params: HoverParams): Hover | null => {
connection.onHover(async (params: HoverParams): Promise<Hover | null> => {
const document = documents.get(params.textDocument.uri);

return document && document.languageId === 'malloy'
? getHover(document, params)
? getHover(document, documents, translateCache, params)
: null;
});

Expand Down