Skip to content

Commit

Permalink
feat(language-server): add Sails completion provider
Browse files Browse the repository at this point in the history
  • Loading branch information
DominusKelvin committed Sep 13, 2024
1 parent 52cbead commit cd7df23
Showing 1 changed file with 63 additions and 0 deletions.
63 changes: 63 additions & 0 deletions packages/language-server/completions/sails-completions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
const lsp = require('vscode-languageserver/node')
const loadSails = require('../helpers/load-sails')

module.exports = async function sailsCompletions(document, position) {
const text = document.getText()
const offset = document.offsetAt(position)
const line = text.substring(0, offset).split('\n').pop()

const match = line.match(/sails((?:\.[a-zA-Z_$][0-9a-zA-Z_$]*)*)\.$/)
if (match) {
try {
return await loadSails(document.uri, (sailsApp) => {
const path = match[1].split('.').filter(Boolean)
return getNestedCompletions(sailsApp, path)
})
} catch (error) {
return []
}
}

return null
}

function getNestedCompletions(obj, path) {
let current = obj
for (const key of path) {
if (current && typeof current === 'object' && key in current) {
current = current[key]
} else {
return []
}
}

if (typeof current !== 'object' || current === null) {
return []
}

const completions = Object.keys(current).map((key) => {
const value = current[key]
let kind = lsp.CompletionItemKind.Property
let detail = 'Property'

if (typeof value === 'function') {
kind = lsp.CompletionItemKind.Method
detail = 'Method'
} else if (typeof value === 'object' && value !== null) {
detail = 'Object'
}

return {
label: key,
kind: kind,
detail: detail,
documentation: `Access to sails${path.length ? '.' + path.join('.') : ''}.${key}`,
sortText: key.startsWith('_') ? `z${key}` : key // Add this line
}
})

// Sort the completions
completions.sort((a, b) => a.sortText.localeCompare(b.sortText))

return completions
}

0 comments on commit cd7df23

Please sign in to comment.