From d61bd0aeff27883a45c1e154a153a6c41620641e Mon Sep 17 00:00:00 2001 From: Kelvin Oghenerhoro Omereshone Date: Fri, 13 Sep 2024 16:16:29 +0100 Subject: [PATCH] feat(language-server): implement go to inertia page --- .../go-to-definitions/go-to-inertia-page.js | 53 +++++++++++++++++++ packages/language-server/index.js | 5 +- 2 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 packages/language-server/go-to-definitions/go-to-inertia-page.js diff --git a/packages/language-server/go-to-definitions/go-to-inertia-page.js b/packages/language-server/go-to-definitions/go-to-inertia-page.js new file mode 100644 index 0000000..6d0f2c2 --- /dev/null +++ b/packages/language-server/go-to-definitions/go-to-inertia-page.js @@ -0,0 +1,53 @@ +const lsp = require('vscode-languageserver/node') +const path = require('path') +const fs = require('fs').promises +const findProjectRoot = require('../helpers/find-project-root') + +module.exports = async function goToInertiaPage(document, position) { + const pageInfo = extractPageInfo(document, position) + + if (!pageInfo) { + return null + } + + const projectRoot = await findProjectRoot(document.uri) + const possibleExtensions = ['.js', '.jsx', '.ts', '.tsx', '.vue', '.svelte'] // Add or remove extensions as needed + + for (const ext of possibleExtensions) { + const fullPagePath = path.join( + projectRoot, + 'assets', + 'js', + 'pages', + `${pageInfo.page}${ext}` + ) + try { + await fs.access(fullPagePath) + return lsp.Location.create(fullPagePath, lsp.Range.create(0, 0, 0, 0)) + } catch (error) { + // File doesn't exist with this extension, try the next one + } + } + + return null +} + +function extractPageInfo(document, position) { + const text = document.getText() + const offset = document.offsetAt(position) + + // Regular expression to match { page: 'example' } or { page: "example" } + const regex = /{\s*page\s*:\s*['"]([^'"]+)['"]\s*}/g + let match + + while ((match = regex.exec(text)) !== null) { + const start = match.index + const end = start + match[0].length + + if (start <= offset && offset <= end) { + return { page: match[1] } + } + } + + return null +} diff --git a/packages/language-server/index.js b/packages/language-server/index.js index 18d3af2..ef45ecb 100644 --- a/packages/language-server/index.js +++ b/packages/language-server/index.js @@ -4,6 +4,7 @@ const validateDocument = require('./validators/validate-document') const goToAction = require('./go-to-definitions/go-to-action') const goToPolicy = require('./go-to-definitions/go-to-policy') const goToView = require('./go-to-definitions/go-to-view') +const goToInertiaPage = require('./go-to-definitions/go-to-inertia-page') const sailsCompletions = require('./completions/sails-completions') const connection = lsp.createConnection(lsp.ProposedFeatures.all) @@ -38,11 +39,13 @@ connection.onDefinition(async (params) => { const actionDefinition = await goToAction(document, params.position) const policyDefinition = await goToPolicy(document, params.position) const viewDefinition = await goToView(document, params.position) + const inertiaPageDefinition = await goToInertiaPage(document, params.position) const definitions = [ actionDefinition, policyDefinition, - viewDefinition + viewDefinition, + inertiaPageDefinition ].filter(Boolean) return definitions.length > 0 ? definitions : null