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

feat(nxls): add document links #1327

Merged
merged 1 commit into from
Aug 24, 2022
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
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import {
ASTNode,
CompletionItem,
JSONDocument,
JSONSchema,
MatchingSchema,
Position,
TextDocument,
} from 'vscode-json-languageservice';
import {
Expand All @@ -16,26 +19,41 @@ import { targetCompletion } from './target-completion';

export async function getCompletionItems(
workingPath: string | undefined,
schema: JSONSchema,
node: ASTNode,
document: TextDocument
jsonAst: JSONDocument,
document: TextDocument,
schemas: MatchingSchema[],
position: Position
): Promise<CompletionItem[]> {
if (!workingPath) {
return [];
}

const items = completionItems(workingPath, node, document);
const offset = document.offsetAt(position);
const node = jsonAst.getNodeFromOffset(offset);
if (!node) {
return [];
}

if (hasCompletionType(schema)) {
const completion = schema[X_COMPLETION_TYPE];
if (hasCompletionGlob(schema)) {
return items(completion, schema[X_COMPLETION_GLOB]);
}
for (const { schema, node: schemaNode } of schemas) {
// Find the schema node that matches the current node
// If the node is found, then we will return the whole function so that we don't have to loop over the rest of the items.
if (schemaNode == node) {
const items = completionItems(workingPath, node, document);

return items(completion);
} else {
return [];
if (hasCompletionType(schema)) {
const completion = schema[X_COMPLETION_TYPE];
if (hasCompletionGlob(schema)) {
return items(completion, schema[X_COMPLETION_GLOB]);
}

return items(completion);
} else {
return [];
}
}
}

return [];
}

function completionItems(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@ import {
CompletionItemKind,
TextDocument,
} from 'vscode-json-languageservice';
import {
isObjectNode,
isPropertyNode,
isStringNode,
} from '../utils/node-types';
import { isStringNode } from '../../utils/node-types';
import { findProjectRoot } from '../../utils/find-project-root';

export async function pathCompletion(
workingPath: string | undefined,
Expand Down Expand Up @@ -72,33 +69,6 @@ export async function pathCompletion(
return items;
}

/**
* Get the first `root` property from the current node to determine `${projectRoot}`
* @param node
* @returns
*/
function findProjectRoot(node: ASTNode): string {
if (isObjectNode(node)) {
for (const child of node.children) {
if (isPropertyNode(child)) {
if (
(child.keyNode.value === 'root' ||
child.keyNode.value === 'sourceRoot') &&
isStringNode(child.valueNode)
) {
return child.valueNode?.value;
}
}
}
}

if (node.parent) {
return findProjectRoot(node.parent);
}

return '';
}

function addCompletionPathItem(
label: string,
path: string,
Expand Down
2 changes: 2 additions & 0 deletions apps/nxls/src/capabilities/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { getCompletionItems } from './completion/get-completion-items';
export { getDocumentLinks } from './links/get-document-links';
63 changes: 63 additions & 0 deletions apps/nxls/src/capabilities/links/get-document-links.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { fileExists } from '@nx-console/file-system';
import {
hasCompletionGlob,
hasCompletionType,
X_COMPLETION_TYPE,
} from '@nx-console/json-schema';
import {
DocumentLink,
JSONDocument,
MatchingSchema,
TextDocument,
Range,
} from 'vscode-json-languageservice';
import { findProjectRoot } from '../../utils/find-project-root';

export async function getDocumentLinks(
workingPath: string | undefined,
jsonAst: JSONDocument,
document: TextDocument,
schemas: MatchingSchema[]
): Promise<DocumentLink[]> {
if (!workingPath) {
return [];
}

const links: DocumentLink[] = [];

if (!jsonAst.root) {
return [];
}

const projectRoot = findProjectRoot(jsonAst.root);
const projectRootPath = workingPath + '/' + projectRoot;

for (const { schema, node } of schemas) {
if (hasCompletionType(schema)) {
const linkType = schema[X_COMPLETION_TYPE];
if (linkType === 'directory') {
continue;
}

const position = document.positionAt(node.offset);
const endPosition = document.positionAt(node.offset + node.length);
const range = Range.create(position, endPosition);

const fullPath = workingPath + '/' + node.value;
if (!(await fileExists(fullPath))) {
continue;
}

if (node.value === projectRoot) {
links.push({
range,
target: projectRootPath,
});
} else {
links.push(DocumentLink.create(range, fullPath));
}
}
}

return links;
}
46 changes: 27 additions & 19 deletions apps/nxls/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
TextDocumentSyncKind,
} from 'vscode-languageserver/node';
import { URI, Utils } from 'vscode-uri';
import { getCompletionItems } from './completions';
import { getCompletionItems, getDocumentLinks } from './capabilities';
import { getLanguageModelCache } from './utils/language-model-cache';
import { getSchemaRequestService } from './utils/runtime';
import { mergeArrays } from './utils/merge-arrays';
Expand Down Expand Up @@ -97,6 +97,10 @@ connection.onInitialize(async (params) => {
triggerCharacters: ['"', ':'],
},
hoverProvider: true,
documentLinkProvider: {
resolveProvider: false,
workDoneProgress: false,
},
},
};

Expand All @@ -118,25 +122,16 @@ connection.onCompletion(async (completionParams) => {
jsonAst
)) ?? CompletionList.create([]);

const offset = document.offsetAt(completionParams.position);
const node = jsonAst.getNodeFromOffset(offset);
if (!node) {
return completionResults;
}

const schemas = await languageService.getMatchingSchemas(document, jsonAst);
for (const s of schemas) {
if (s.node === node) {
const pathItems = await getCompletionItems(
WORKING_PATH,
s.schema,
node,
document
);
mergeArrays(completionResults.items, pathItems);
break;
}
}

const pathItems = await getCompletionItems(
WORKING_PATH,
jsonAst,
document,
schemas,
completionParams.position
);
mergeArrays(completionResults.items, pathItems);

return completionResults;
});
Expand All @@ -152,6 +147,19 @@ connection.onHover(async (hoverParams) => {
return languageService.doHover(document, hoverParams.position, jsonAst);
});

connection.onDocumentLinks(async (params) => {
const linkDocument = documents.get(params.textDocument.uri);

if (!linkDocument) {
return null;
}

const { jsonAst, document } = getJsonDocument(linkDocument);
const schemas = await languageService.getMatchingSchemas(document, jsonAst);

return getDocumentLinks(WORKING_PATH, jsonAst, document, schemas);
});

const jsonDocumentMapper = getLanguageModelCache(10, 60, (document) =>
languageService.parseJSONDocument(document)
);
Expand Down
29 changes: 29 additions & 0 deletions apps/nxls/src/utils/find-project-root.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { ASTNode } from 'vscode-json-languageservice';
import { isObjectNode, isPropertyNode, isStringNode } from './node-types';

/**
* Get the first `root` property from the current node to determine `${projectRoot}`
* @param node
* @returns
*/
export function findProjectRoot(node: ASTNode): string {
if (isObjectNode(node)) {
for (const child of node.children) {
if (isPropertyNode(child)) {
if (
(child.keyNode.value === 'root' ||
child.keyNode.value === 'sourceRoot') &&
isStringNode(child.valueNode)
) {
return child.valueNode?.value;
}
}
}
}

if (node.parent) {
return findProjectRoot(node.parent);
}

return '';
}