import sublime_plugin
import sublime
import re

class MmixAutocomplete(sublime_plugin.EventListener):
    def on_query_completions(self, view, prefix, locations):
        if view.settings().get("syntax").endswith("mmix.sublime-syntax"):
            words = []
            previous = view.substr(sublime.Region(0, locations[0]))

            pos = max(previous.rfind(";"), previous.rfind("\n"), previous.rfind("\r")) + 1
            arg = len(re.findall(r'[ \t]+', previous[pos:]))

            if arg == 0:
                words.append(("Main", "Main "))

            if arg == 1:
                words.append(("ADD", "ADD "))
                words.append(("SUB", "SUB "))
                words.append(("MUL", "MUL "))
                words.append(("DIV", "DIV "))
                words.append(("ADDU", "ADDU "))
                words.append(("SUBU", "SUBU "))
                words.append(("MULU", "MULU "))
                words.append(("DIVU", "DIVU "))

                words.append(("FADD", "FADD "))
                words.append(("FSUB", "FSUB "))
                words.append(("FMUL", "FMUL "))
                words.append(("FDIV", "FDIV "))

                words.append(("TRAP", "TRAP "))
                words.append(("TRIP", "TRIP "))
                words.append(("RESUME", "RESUME "))

                words.append(("FCMP", "FCMP "))
                words.append(("FUN", "FUN "))
                words.append(("FEQL", "FEQL "))
                words.append(("FIX", "FIX "))
                words.append(("FIXU", "FIXU "))
                words.append(("FLOT", "FLOT "))
                words.append(("FLOTU", "FLOTU "))
                words.append(("SFLOT", "SFLOT "))
                words.append(("SFLOTU", "SFLOTU "))
                words.append(("FCMPE", "FCMPE "))
                words.append(("FUNE", "FUNE "))
                words.append(("FEQLE", "FEQLE "))
                words.append(("FSQRT", "FSQRT "))
                words.append(("FREM", "FREM "))
                words.append(("FINT", "FINT "))
                words.append(("2ADDU", "2ADDU "))
                words.append(("4ADDU", "4ADDU "))
                words.append(("8ADDU", "8ADDU "))
                words.append(("16ADDU", "16ADDU "))
                words.append(("CMP", "CMP "))
                words.append(("CMPU", "CMPU "))
                words.append(("NEG", "NEG "))
                words.append(("NEGU", "NEGU "))
                words.append(("SL", "SL "))
                words.append(("SLU", "SLU "))
                words.append(("SR", "SR "))
                words.append(("SRU", "SRU "))
                words.append(("BN (negative)", "BN "))
                words.append(("BZ (zero)", "BZ "))
                words.append(("BP (positive)", "BP "))
                words.append(("BOD (odd)", "BOD "))
                words.append(("BNN (nonnegative)", "BNN "))
                words.append(("BNZ (nonzero)", "BNZ "))
                words.append(("BNP (nonpositive)", "BNP "))
                words.append(("BEV (even)", "BEV "))
                words.append(("PBN (negative)", "PBN "))
                words.append(("PBZ (zero)", "PBZ "))
                words.append(("PBP (positive)", "PBP "))
                words.append(("PBOD (odd)", "PBOD "))
                words.append(("PBNN (nonnegative)", "PBNN "))
                words.append(("PBNZ (nonzero)", "PBNZ "))
                words.append(("PBNP (nonpositive)", "PBNP "))
                words.append(("PBEV (even)", "PBEV "))
                words.append(("CSN", "CSN "))
                words.append(("CSZ", "CSZ "))
                words.append(("CSP", "CSP "))
                words.append(("CSOD", "CSOD "))
                words.append(("CSNN", "CSNN "))
                words.append(("CSNZ", "CSNZ "))
                words.append(("CSNP", "CSNP "))
                words.append(("CSEV", "CSEV "))
                words.append(("ZSN", "ZSN "))
                words.append(("ZSZ", "ZSZ "))
                words.append(("ZSP", "ZSP "))
                words.append(("ZSOD", "ZSOD "))
                words.append(("ZSNN", "ZSNN "))
                words.append(("ZSNZ", "ZSNZ "))
                words.append(("ZSNP", "ZSNP "))
                words.append(("ZSEV", "ZSEV "))
                words.append(("LDB", "LDB "))
                words.append(("LDBU", "LDBU "))
                words.append(("LDW", "LDW "))
                words.append(("LDWU", "LDWU "))
                words.append(("LDT", "LDT "))
                words.append(("LDTU", "LDTU "))
                words.append(("LDO", "LDO "))
                words.append(("LDOU", "LDOU "))
                words.append(("LDSF", "LDSF "))
                words.append(("LDHT", "LDHT "))
                words.append(("CSWAP", "CSWAP "))
                words.append(("LDUNC", "LDUNC "))
                words.append(("LDVTS", "LDVTS "))
                words.append(("PRELD", "PRELD "))
                words.append(("PREGO", "PREGO "))
                words.append(("GO", "GO "))
                words.append(("STB", "STB "))
                words.append(("STBU", "STBU "))
                words.append(("STW", "STW "))
                words.append(("STWU", "STWU "))
                words.append(("STT", "STT "))
                words.append(("STTU", "STTU "))
                words.append(("STO", "STO "))
                words.append(("STOU", "STOU "))
                words.append(("STSF", "STSF "))
                words.append(("STHT", "STHT "))
                words.append(("STCO", "STCO "))
                words.append(("STUNC", "STUNC "))
                words.append(("SYNCD", "SYNCD "))
                words.append(("PREST", "PREST "))
                words.append(("SYNCID", "SYNCID "))
                words.append(("PUSHGO", "PUSHGO "))
                words.append(("OR", "OR "))
                words.append(("ORN", "ORN "))
                words.append(("NOR", "NOR "))
                words.append(("XOR", "XOR "))
                words.append(("AND", "AND "))
                words.append(("ANDN", "ANDN "))
                words.append(("NAND", "NAND "))
                words.append(("NXOR", "NXOR "))
                words.append(("BDIF", "BDIF "))
                words.append(("WDIF", "WDIF "))
                words.append(("TDIF", "TDIF "))
                words.append(("ODIF", "ODIF "))
                words.append(("MUX", "MUX "))
                words.append(("SADD", "SADD "))
                words.append(("MOR", "MOR "))
                words.append(("MXOR", "MXOR "))
                words.append(("SETH", "SETH "))
                words.append(("SETMH", "SETMH "))
                words.append(("SETML", "SETML "))
                words.append(("SETL", "SETL "))
                words.append(("INCH", "INCH "))
                words.append(("INCMH", "INCMH "))
                words.append(("INCML", "INCML "))
                words.append(("INCL", "INCL "))
                words.append(("ORH", "ORH "))
                words.append(("ORMH", "ORMH "))
                words.append(("ORML", "ORML "))
                words.append(("ORL", "ORL "))
                words.append(("ANDNH", "ANDNH "))
                words.append(("ANDNMH", "ANDNMH "))
                words.append(("ANDNML", "ANDNML "))
                words.append(("ANDNL", "ANDNL "))
                words.append(("JMP", "JMP "))
                words.append(("PUSHJ", "PUSHJ "))
                words.append(("GETA", "GETA "))
                words.append(("PUT", "PUT "))
                words.append(("POP", "POP "))
                words.append(("SAVE", "SAVE "))
                words.append(("UNSAVE", "UNSAVE "))
                words.append(("SYNC", "SYNC "))
                words.append(("SWYM", "SWYM "))
                words.append(("GET", "GET "))
                words.append(("SET", "SET "))
                words.append(("LDA", "LDA "))
                words.append(("IS", "IS "))
                words.append(("LOC", "LOC "))
                words.append(("PREFIX", "PREFIX "))
                words.append(("BYTE", "BYTE "))
                words.append(("WYDE", "WYDE "))
                words.append(("TETRA", "TETRA "))
                words.append(("OCTA", "OCTA "))
                words.append(("BSPEC", "BSPEC "))
                words.append(("ESPEC", "ESPEC "))
                words.append(("GREG", "GREG "))
                words.append(("LOCAL", "LOCAL "))

            if arg == 2:
                words.extend(map(lambda x: (x, x), list(set(re.findall(r'^[0-9a-zA-Z][^ \t]*', view.substr(sublime.Region(0, view.size())), re.M)))))

                words.append(("Inf", "Inf"))
                words.append(("Data_Segment", "Data_Segment"))
                words.append(("Pool_Segment", "Pool_Segment"))
                words.append(("Stack_Segment", "Stack_Segment"))
                words.append(("D_BIT", "D_BIT"))
                words.append(("V_BIT", "V_BIT"))
                words.append(("W_BIT", "W_BIT"))
                words.append(("I_BIT", "I_BIT"))
                words.append(("O_BIT", "O_BIT"))
                words.append(("U_BIT", "U_BIT"))
                words.append(("Z_BIT", "Z_BIT"))
                words.append(("X_BIT", "X_BIT"))
                words.append(("D_Handler", "D_Handler"))
                words.append(("V_Handler", "V_Handler"))
                words.append(("W_Handler", "W_Handler"))
                words.append(("I_Handler", "I_Handler"))
                words.append(("O_Handler", "O_Handler"))
                words.append(("U_Handler", "U_Handler"))
                words.append(("Z_Handler", "Z_Handler"))
                words.append(("X_Handler", "X_Handler"))
                words.append(("StdIn", "StdIn"))
                words.append(("StdOut", "StdOut"))
                words.append(("StdErr", "StdErr"))
                words.append(("TextRead", "TextRead"))
                words.append(("TextWrite", "TextWrite"))
                words.append(("BinaryRead", "BinaryRead"))
                words.append(("BinaryWrite", "BinaryWrite"))
                words.append(("BinaryReadWrite", "BinaryReadWrite"))
                words.append(("Halt", "Halt"))
                words.append(("Fopen", "Fopen"))
                words.append(("Fclose", "Fclose"))
                words.append(("Fread", "Fread"))
                words.append(("Fgets", "Fgets"))
                words.append(("Fgetws", "Fgetws"))
                words.append(("Fwrite", "Fwrite"))
                words.append(("Fputs", "Fputs"))
                words.append(("Fputws", "Fputws"))
                words.append(("Fseek", "Fseek"))
                words.append(("Ftell", "Ftell"))
                words.append(("ROUNT_CURRENT", "ROUNT_CURRENT"))
                words.append(("ROUND_OFF", "ROUND_OFF"))
                words.append(("ROUND_UP", "ROUND_UP"))
                words.append(("ROUND_DOWN", "ROUND_DOWN"))
                words.append(("ROUND_NEAR", "ROUND_NEAR"))
                words.append(("rB", "rB"))
                words.append(("rD", "rD"))
                words.append(("rE", "rE"))
                words.append(("rH", "rH"))
                words.append(("rJ", "rJ"))
                words.append(("rM", "rM"))
                words.append(("rR", "rR"))
                words.append(("rC", "rC"))
                words.append(("rN", "rN"))
                words.append(("rO", "rO"))
                words.append(("rS", "rS"))
                words.append(("rI", "rI"))
                words.append(("rT", "rT"))
                words.append(("rK", "rK"))
                words.append(("rQ", "rQ"))
                words.append(("rU", "rU"))
                words.append(("rV", "rV"))
                words.append(("rG", "rG"))
                words.append(("rL", "rL"))
                words.append(("rA", "rA"))
                words.append(("rF", "rF"))
                words.append(("rP", "rP"))
                words.append(("rW", "rW"))
                words.append(("rX", "rX"))
                words.append(("rY", "rY"))
                words.append(("rZ", "rZ"))
                words.append(("rBB", "rBB"))
                words.append(("rTT", "rTT"))
                words.append(("rWW", "rWW"))
                words.append(("rXX", "rXX"))
                words.append(("rYY", "rYY"))
                words.append(("rZZ", "rZZ"))
            return (words, sublime.INHIBIT_WORD_COMPLETIONS)
