Skip to content

Commit

Permalink
Merge pull request #16 from sailshq/feat/go-to-inertia-page
Browse files Browse the repository at this point in the history
feat(language-server): implement go to inertia page
  • Loading branch information
DominusKelvin authored Sep 13, 2024
2 parents 2bae818 + d61bd0a commit fea1dd7
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
53 changes: 53 additions & 0 deletions packages/language-server/go-to-definitions/go-to-inertia-page.js
Original file line number Diff line number Diff line change
@@ -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
}
5 changes: 4 additions & 1 deletion packages/language-server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit fea1dd7

Please sign in to comment.