-
-
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 #17 from sailshq/feat/go-to-helper
[feat]: go to helper
- Loading branch information
Showing
4 changed files
with
79 additions
and
18 deletions.
There are no files selected for viewing
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
47 changes: 47 additions & 0 deletions
47
packages/language-server/go-to-definitions/go-to-helper.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,47 @@ | ||
const lsp = require('vscode-languageserver/node') | ||
const path = require('path') | ||
const fs = require('fs').promises | ||
const findProjectRoot = require('../helpers/find-project-root') | ||
const findFnLine = require('../helpers/find-fn-line') | ||
|
||
module.exports = async function goToHelper(document, position) { | ||
const helperInfo = extractHelperInfo(document, position) | ||
|
||
if (!helperInfo) { | ||
return null | ||
} | ||
|
||
const projectRoot = await findProjectRoot(document.uri) | ||
const fullHelperPath = | ||
path.join(projectRoot, 'api', 'helpers', ...helperInfo.helperPath) + '.js' | ||
|
||
if (fullHelperPath) { | ||
const fnLineNumber = await findFnLine(fullHelperPath) | ||
return lsp.Location.create( | ||
fullHelperPath, | ||
lsp.Range.create(fnLineNumber, 0, fnLineNumber, 0) | ||
) | ||
} | ||
} | ||
|
||
function extractHelperInfo(document, position) { | ||
const text = document.getText() | ||
const offset = document.offsetAt(position) | ||
|
||
// Regular expression to match sails.helpers.exampleHelper() or sails.helpers.exampleHelper.with() | ||
// Also matches nested helpers like sails.helpers.mail.send() or sails.helpers.mail.send.with() | ||
const regex = /sails\.helpers\.([a-zA-Z0-9.]+)(?:\.with)?\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) { | ||
const helperPath = match[1].split('.').filter((part) => part !== 'with') | ||
return { helperPath } | ||
} | ||
} | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
const fs = require('fs').promises | ||
const url = require('url') | ||
|
||
module.exports = async function findFnLine(filePath) { | ||
try { | ||
const resolvedPath = filePath.startsWith('file:') | ||
? url.fileURLToPath(filePath) | ||
: filePath | ||
|
||
const content = await fs.readFile(resolvedPath, 'utf8') | ||
const lines = content.split('\n') | ||
for (let i = 0; i < lines.length; i++) { | ||
if (lines[i].includes('fn:')) { | ||
return i // Return the line number (0-based index) | ||
} | ||
} | ||
return 0 // If 'fn:' is not found, return the first line | ||
} catch (error) { | ||
return 0 // Return the first line if there's an error | ||
} | ||
} |
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