-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from sailshq/feat/go-to-inertia-page
feat(language-server): implement go to inertia page
- Loading branch information
Showing
2 changed files
with
57 additions
and
1 deletion.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
packages/language-server/go-to-definitions/go-to-inertia-page.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters