0

Snippet to generate custom JavaScript CodeCompletion file

Tapsi 6 years ago updated 6 years ago 3

Hi guys,


I wrote a small snippet to generate custom JS code completion description files. It can be executed at runtime and parses all objects from the global object and tries to extract their API.


I know at the moment Textastic does not supports suggestion of file symbols nor project wide symbols. That's why this snippets maybe helps people to workaround that missing feature.

At the moment this snippet is very limited and only makes the method names available in the editor without any context. I plan to add the context information and maybe also the function parameters for the completion data.  


So if you're interested in this snippet, then I can share future versions of the script here. :) 




const logCompletionDescriptionData = () => {
          const getSymbols = function() {
            const getSymbolsOfObject = (obj, symbols = [], level = 0) => {
              if (level == 5) return []
              return Object
                .keys(obj)
                .filter(key => symbols.indexOf(key) === -1)
                .filter(key => isNaN(parseInt(key)))
                .map((key, i, array) => {
                  const value = obj[key]
                  if (!value) return [key]
                  
                  const isArray = Array.isArray(value)
                  const isFunction = typeof value === "function"
                  
                  if (!isArray && !isFunction) {
                    return [key].concat(getSymbolsOfObject(value, array, level + 1))
                  }
                  
                  return [key]
                })
                .reduce((a, b) => a.concat(b), [])
                .filter((value, index, self) => self.indexOf(value) === index)
                .sort((a,b) => a < b ? -1 : +1)
            }
              
            return getSymbolsOfObject(window) 
          }     
          
          const metaData = {
            "description": "BlackCats Code",
            "uuid": "DE267669-E66E-400A-ACEA-BD906DC2B813",
            "completionSets": [
              {
                "name": "js.blackcat.code",
                "strings": getSymbols()
              }
            ],
            "contexts": [
              {
                "description": "root",
                "scope": "source.js - comment - string",
                "pattern": "(?<!\\.)\\b([a-zA-Z]*)",
                "completionCaptureIndex": 1,
                "completionSetNames": [
                  "js.blackcat.code"
                ]
              }
            ]
          }
          
          console.log(JSON.stringify(metaData, null, 2))
      }




Wow, that's pretty cool! 👍

Hi Tapsi, this sounds like an awesome feature.

But I can not say for sure, as I don't have any idea how to get the snippet to work.

Could you help me and probably others in describing how to get it to work ?


And yes, please post further versions ;-)


Thanks for your post,

Thomas

@thomas I am on vacation next week. So I'll answer when I am back at home. :)