helsmy / vscode-autohotkey

A simple Autohotkey language server with some advanced features
https://marketplace.visualstudio.com/items?itemName=helsmy.ahk-simple-ls
GNU Lesser General Public License v3.0
7 stars 3 forks source link

Defining a method with a string #15

Closed CzarOfScripts closed 1 year ago

CzarOfScripts commented 2 years ago

For strings you can add a method to the "base", I know you're kind of rewriting the whole parsing, so it would be nice if you take that into account.

Code example:

"".base.toUpperCase := func("strUpper")

strUpper(str)
{
    stringUpper, str, str

    return str
}

Usage:

msgbox, % "Test".toUpperCase() ; Output: TEST
helsmy commented 1 year ago

This feature is on the plan list. But, it will not come very soon, since v2 has become the primary version and v2 syntax supporting will take a lot of time. By the way, the parser refactoring is now complete, feel free to try it out!

CzarOfScripts commented 1 year ago

Can you tell me what's new in the parser? Or should I just check for errors?

helsmy commented 1 year ago

It is more error tolerant and precise than old one. Such as, some code like this. 图片 The new parser can give some reasonable result than old one. But, since this is the first release of the new parser, there are still many problems

CzarOfScripts commented 1 year ago

Outline:

No symbols found in document

#Persistent
#SingleInstance FORCE
#MaxThreads
#MaxHotkeysPerInterval 200
SetBatchLines, -1
SetWorkingDir %A_ScriptDir%
#NoEnv

OnExit("closeScript")

;* Includes
#include D:/AutoHotKey/library/codeProtect.ahk
#include D:/AutoHotKey/library/runFromAdmin.ahk
#include D:/AutoHotKey/library/VA.ahk
; Others
#include D:/AutoHotKey/library/Others/darkTray.ahk
#include D:/AutoHotKey/library/Others/inputBox.ahk
; Gui
#include D:/AutoHotKey/library/Gui/guiControlSetText.ahk
; Files
#include D:/AutoHotKey/library/Files/fileAppend.ahk
#include D:/AutoHotKey/library/Files/fileDelete.ahk
#include D:/AutoHotKey/library/Files/filePrepend.ahk
#include D:/AutoHotKey/library/Files/fileRead.ahk
#include D:/AutoHotKey/library/Files/isFile.ahk
#include D:/AutoHotKey/library/Files/isFolder.ahk
#include D:/AutoHotKey/library/Files/watchFolder.ahk
; Strings
#include D:/AutoHotKey/library/Strings/strLower.ahk
#include D:/AutoHotKey/library/Strings/strRepeat.ahk
#include D:/AutoHotKey/library/Strings/strUpper.ahk
; Class
#include D:/AutoHotKey/library/Class/Array.ahk
#include D:/AutoHotKey/library/Class/Clipboard.ahk
#include D:/AutoHotKey/library/Class/Map.ahk
#include D:/AutoHotKey/library/Class/Math.ahk
#include D:/AutoHotKey/library/Class/Message.ahk
#include D:/AutoHotKey/library/Class/Object.ahk
#include D:/AutoHotKey/library/Class/Set.ahk
#include D:/AutoHotKey/library/Class/Timer.ahk
; Convert
#include D:/AutoHotKey/library/Convert/timeToObject.ahk

;* Menu Tray
menu, tray, NoStandard
menu, tray, icon, shell32.dll, 42

menu, contacts, add , % "Czar Of Scripts | My site"            , authorSite
menu, contacts, add , % "Czar Of Scripts | I'm in VK"          , authorVK
menu, contacts, add , % "Czar Of Scripts | I'm on Cheat-Master", authorCM
menu, contacts, icon, % "Czar Of Scripts | My site"            , shell32.dll, 264
menu, contacts, icon, % "Czar Of Scripts | I'm in VK"          , shell32.dll, 264
menu, contacts, icon, % "Czar Of Scripts | I'm on Cheat-Master", shell32.dll, 264

menu, tray, tip, % "Czar Of Scripts | AutoRun"
; Menu, Tray, Add, % "Cheat Master - Helper", runHelper
Menu, Tray, Add, % "Czar Of Scripts", closeScript
Menu, Tray, disable, % "Czar Of Scripts"
if (A_IsCompiled)
{
    menu, tray, icon, % "Czar Of Scripts", % A_ScriptFullPath, 1
}
Menu, Tray, add
; Menu, Tray, add
Menu, Tray, Add , % "Turn off computer", offComp
Menu, Tray, add
menu, tray, add , % "Contacts"         , :contacts
menu, tray, icon, % "Contacts"         , shell32.dll, 161
menu, tray, add
menu, tray, add , % "Close script"     , closeScript
menu, tray, icon, % "Close script"     , shell32.dll, 132

loadTime := timeToObject(A_TickCount)
TrayTip, % "Auto Run", % "Windows starts up in " loadTime.minutes ":" loadTime.seconds "." loadTime.ms, 2, 17

global capsLockState := false
global _global := {autoConnectInternet: 0, runAhkScripts: 0, moveMTA: 0}

SetNumLockState, AlwaysOn
SetCapsLockState, AlwaysOff

onClipboardChange("clipboardChange")

; SetTimer, CheckInternet, 30000
; SetTimer, clearMemory, % 15 * 60 * 1000
; CheckInternet()
; clearMemory()
SetTimer, CheckWinSize, 300
CheckWinSize()

if (!winExist("ahk_exe chrome.exe"))
{
    result := Message.question("Run browser?", "AutoRun", 0x4, 5)

    if (result == "Yes" || result == "Timeout")
    {
        Run, % "C:\Users\" A_UserName "\AppData\Local\Google\Chrome SxS\Application\chrome.exe"
    }
}

if (!watchFolder("D:\Downloads", "fileActionHandler",, 3))
{
    Message.error("fileActionHandler")
}

#capsLock::
SetCapsLockState, % "Always" ((capsLockState := !capsLockState) ? "On" : "Off")
return

fileActionHandler(folder, changes)
{
    for k, change in changes
    {
        if (change.action != 1 && change.action != 4)
        {
            continue
        }

        fileName := change.name

        if (fileName ~= "\.mp3$")
        {
            if (fileName ~= "_320kbps")
            {
                fileName := RegExReplace(fileName, "_320kbps")
            }
            if (fileName ~= "y2mate\.com \- ")
            {
                fileName := RegExReplace(fileName, "y2mate\.com \- ")
            }

            if (fileName != change.name)
            {
                sleep, 200
                fileMove, % change.name, % fileName, 1
            }
        }
    }
}

clipboardChange(type)
{
    switch (type)
    {
        case 1:
        {
            clip_text := ClassClipboard.getText()

            if ((pos := RegExMatch(clip_text, "Pawno-Info\.Ru\.")))
            {
                ClassClipboard.copyText(SubStr(clip_text, 1, pos - 5))
            }
            else if (StrSplit(clip_text, "`r`n").maxIndex() > 1)
            {
                return
            }
            else if (RegExMatch(clip_text, "^(?:http[s]?:\/\/)?cheat-master\.ru(.*)", out))
            {
                ClassClipboard.copyText("https://CzarOfScripts.com/redirect.php?url=https://cheat-master.ru" out1)
            }
            else if (pos := RegExMatch(clip_text, "(Переведено с помощью|Translated with)"))
            {
                ClassClipboard.copyText(SubStr(clip_text, 1, pos - 3))
            }
            else if (RegExMatch(clip_text, "https:\/\/www\.youtube\.com\/shorts\/(.*)", out))
            {
                ClassClipboard.copyText("https://www.youtube.com/watch?v=" out1)
            }
        }
        case 2:
        {
            WinGetTitle, title, A

            if (title == "Screenpic")
            {
                WinGet, processExe, ProcessName, A

                if (processExe == "screenpic.exe")
                {
                    WinClose, A
                    return
                }
            }
        }
    }
}

clearMemory()
{
    Run, % "D:\Utilities\EmptyStandbyList.exe",, HIDE
}

offComp()
{
    Shutdown, 8
}

; autoConnectInternet()
; {
;   _global.autoConnectInternet := !_global.autoConnectInternet
;   Menu, Tray, Rename, % "���� ����������� � ��������� (" (_global.autoConnectInternet ? "off" : "on") ")", % "���� ����������� � ��������� (" (_global.autoConnectInternet ? "on" : "off") ")"
; }

; changeOutputAudio()
; {
;   SwapAudioDevice("LG FULL HD-4", "��������")
; }

CheckWinSize()
{
    static lastTitle := ""

    WinGetTitle, title, A
    WinGetClass, class, A
    WinGetPos, X, Y, Width, Height, A

    if (lastTitle == title)
    {
        return
    }

    lastTitle := title

    switch (title)
    {
        case "Caliber Launcher":
        {
            WinGet, processExe, ProcessName, A

            if (processExe == "Launcher.exe")
            {
                CoordMode, Mouse, Window
                sleep, 200
                Mouse.click("L", 568, 610)
            }
        }
        case "ВНИМАНИЕ: данный драйвер видеоадаптера может вызывать проблемы.":
        {
            ControlClick, OK, % title
        }
        case "TopTracker":
        {
            if (Height == 600)
            {
                WinMove, TopTracker,, 3688, 780, 350, 270
            }
        }
    }
}

~^vk57::
WinGetTitle, title, A

if (title == "Untitled.txt ?- Notepads")
{
    WinKill, A
}
return

#down::
WinMinimize, A
return

#up::
WinKill, A
return

^!vk41:: ; Alt+Ctrl+A
convertLayout()
return

convertLayout()
{
    static letter := {"ф": "a", "и": "b", "с": "c", "в": "d", "у": "e", "а": "f", "п": "g", "р": "h", "ш": "i", "о": "j", "л": "k", "д": "l", "ь": "m", "т": "n", "щ": "o", "з": "p", "й": "q", "к": "r", "ы": "s", "е": "t", "г": "u", "м": "v", "ц": "w", "ч": "x", "н": "y", "я": "z", "a": "ф", "b": "и", "c": "с", "d": "в", "e": "у", "f": "а", "g": "п", "h": "р", "i": "ш", "j": "о", "k": "л", "l": "д", "m": "ь", "n": "т", "o": "щ", "p": "з", "q": "й", "r": "к", "s": "ы", "t": "е", "u": "г", "v": "м", "w": "ц", "x": "ч", "y": "н", "z": "я"}

    Send, ^{vk43}
    sleep, 100
    clipboardText := ClassClipboard.getText()
    if (!clipboardText)
        return

    out := ""

    loop, parse, clipboardText
    {
        out .= (letter[A_LoopField] ? letter[A_LoopField] : A_LoopField)
    }

    ClassClipboard.copyText(out)
    sleep, 100
    Send, ^{vk56}
}

^!vk44::
Send, {CTRL down}{vk43 down}{vk43 up}{CTRL up}
sleep, 100
MakeAutoIndent(ClassClipboard.getText())
SoundBeep,, 500
Send, {CTRL down}{vk56 down}{vk56 up}{CTRL up}
Return

SwapAudioDevice(device_A, device_B)
{
    ; Get device IDs.
    A := VA_GetDevice(device_A), VA_IMMDevice_GetId(A, A_id)
    B := VA_GetDevice(device_B), VA_IMMDevice_GetId(B, B_id)
    if A && B
    {
        ; Get ID of default playback device.
        default := VA_GetDevice("playback")
        VA_IMMDevice_GetId(default, default_id)
        ObjRelease(default)
        ; If device A is default, set device B; otherwise set device A.
        VA_SetDefaultEndpoint(default_id == A_id ? B : A, 0)
    }
    ObjRelease(B)
    ObjRelease(A)
    if !(A && B)
        throw Exception("Unknown audio device", -1, A ? device_B : device_A)
    Return default_id == A_id ? device_B : device_A
}

MakeAutoIndent(filePath)
{
    source := "", line := "", brackets := 0, oneBracket := false, ignore := false
    Loop, Parse, % filePath, `n
    {
        line := Trim(A_LoopField)
        indent := ""
        if (StrStartsWith(line, "(") && !InStr(line, ")"))
            ignore := true
        else if (StrStartsWith(line, ")") && !InStr(line, "("))
            ignore := false
        if (!ignore)
        {
            if (StrScan(line, "}") && !InStr(line, "{"))
                brackets--
            Loop, % brackets
                indent := "`t" indent
            if (oneBracket)
            {
                oneBracket := false
                if (!StrScan(line, "{"))
                    indent := "`t" indent
            }
            if (StrScan(line, "{") && !InStr(line, "}"))
                brackets++
            if (IsActs(line) && !StrScan(str, "{"))
                oneBracket := true
        }
        source .= (ignore ? A_LoopField : indent line) "`n"
    }

    ClassClipboard.copyText(source)
}

IsActs(str)
{
    acts := [ "if", "else", "if else", "while", "loop", "for", "try", "catch", "finally"
            , "IfEqual", "IfNotEqual", "IfLess", "IfLessOrEqual", "IfGreater", "IfGreaterOrEqual"
            , "IfExist", "IfNotExist", "IfInString", "IfNotInString", "IfMsgBox", "IfWinActive"
            , "IfWinNotActive", "IfWinExist", "IfWinNotExist"]
    for k, v in acts
    {
        if (StrStartsWith(str, v))
            return true
    }
    return false
}

StrScan(str, cmd)
{
    return StrStartsWith(str, cmd) || StrEndsWith(str, cmd)
}

StrStartsWith(str, cmd)
{
    return SubStr(str, 1, StrLen(cmd)) == cmd
}

StrEndsWith(str, cmd)
{
    return SubStr(str, 0, StrLen(cmd)) == cmd
}

; ----------------
authorSite()
{
    run, % "https://CzarOfScripts.com"
}

authorVK()
{
    run, % "https://vk.com/id173241815"
}

authorCM()
{
    run, % "https://cheat-master.ru/index/8-459193"
}

closeScript()
{
    closeScript:

    watchFolder("D:\Downloads", "**DEL")

    ExitApp
}
CzarOfScripts commented 1 year ago

In v0.9.1 all seems to be displayed, but now in outline does not show modifiers in the hotkey, that is, if there is vk57:: and somewhere else !vk57, only the latest in the code will be displayed and will sign "vk57".

CzarOfScripts commented 1 year ago

The problem above has been solved by update v0.9.1, thank you!