CodyKochmann
2/6/2016 - 2:32 PM

autocomplete-generator.py

autocomplete-generator.py

# coding:      utf-8
# author:      Cody Kochmann
# date:        Sat 06 Feb 2016 09:30:29 AM EST
# description: interactively make sublime text completitions

""" sample json for .sublime-completions file
{
        "scope": "text.html - source - meta.tag, punctuation.definition.tag.begin",

        "completions":
        [
                { "trigger": "a", "contents": "<a href=\"$1\">$0</a>" },
                { "trigger": "abbr", "contents": "<abbr>$0</abbr>" },
                { "trigger": "acronym", "contents": "<acronym>$0</acronym>" },

                "ninja",
                "robot",
                "pizza"
        ]
}
"""

global autocomplete_dictionary
autocomplete_dictionary=[]

class Autocomplete_Dictionary:
  def __init__(self, save_path=''):
    self.completition

         
import json

class Completion:
  def __init__(self, scope='', contents='', trigger=''):
    self.data={}
    self.data['scope']=scope
    self.data['completions'] = []
    
    if trigger != '':
      completion_data={}
      completion_data['trigger']=trigger
      completion_data['contents']=contents
      self.data['completions'].append(completion_data)
    else:
      self.data['completions'].append(contents)
    
    self.to_string=(json.dumps(self.data))


def remove_all(substring='',target=''):
  if substring in target:
    target = ''.join(target.split(substring))
  return target



def append_to_file(s='',path=''):
  with open(path,'a') as f:
    f.write(s+'\n')

def write_to_file(s='',path=''):
  with open(path,'w') as f:
    f.write(s)
    
def read_file(path=''):
  with open(path,'r') as f:
    return f.read()
    
def random_hash(length=256):
  import random
  return random.getrandbits(length)
  
file_name='sublime-completions/'+str(random_hash())+'.sublime-completions'

contents=raw_input('enter the completition ( one line ): ')
trigger=raw_input('enter the trigger ( leave blank if none ): ')
scope=raw_input('enter the scope ( example: py ): ')
snippet = Completion(scope,contents,trigger)

write_to_file(snippet.to_string, file_name)