Jump to content

[solved] AutoCloseBraces.lua for SciTE


Recommended Posts

Moin,

I want to add a ''AutoCloseBraces.lua" to - who'll guess it - autoclose braces/brackets and qoutes.

I've added the filename to the filelist in SciTEStartup.lua and copied it into the SciTE\LUA directory.

But it doesn't work.

Here's the code

function OnChar(charAdded)
 -- trace(charAdded)
     local toClose = { ['('] = ')', ['{'] = '}', ['['] = ']', ['"'] = '"', ["'"] = "'" }
 
     if toClose[charAdded] ~= nil then
         local pos = editor.CurrentPos
         editor:ReplaceSel(toClose[charAdded])
         editor:SetSel(pos, pos)
     end
     return false   -- Let next handler to process event
 -- return true  -- Don't let next handler to process event
 end
 -- http://osdir.com/ml/lib.scintilla.devel/2005-01/msg00046.html

Could anybody give me a tip how to realize this, please ?

Greetz

Greenhorn

Edited by Greenhorn
Link to comment
Share on other sites

OK, I got it working !

if somebody else is interested how to get it working, then add the following line to SciTEStartup.lua

LoadLuaFile("AutoCloseBraces.lua")

Now the SciTEStartup.lua should look like this

--------------------------------------------------------------------------------
  -- SciTE startup script.
  --------------------------------------------------------------------------------
  
  -- A table listing all loaded files.
  LoadLuaFileList = { }
  
  --------------------------------------------------------------------------------
  -- LoadLuaFile(file, directory)
  --
  -- Helper function for easily loading Lua files.
  --
  -- Parameters:
  --    file - The name of a Lua file to load.
  --    directory - If specified, file is looked for in that directory.  By default,
  --         this directory is $(SciTEDefaultHome)\Lua.
  --------------------------------------------------------------------------------
  function LoadLuaFile(file, directory)
      if directory == nil then
          directory = props["SciteDefaultHome"] .. "\\Lua\\"
      end
      table.insert(LoadLuaFileList, directory .. file)
      dofile(directory .. file)
  end   -- LoadLuaFile()
  
  -- Load all the Lua files.
  LoadLuaFile("Class.lua")  -- Always load first.
  LoadLuaFile("Common.lua") -- Always load second.
  LoadLuaFile("AutoItPixmap.lua")
  LoadLuaFile("AutoHScroll.lua")
  LoadLuaFile("AutoItAutoComplete.lua")
  LoadLuaFile("LoadSession.lua")
  LoadLuaFile("AutoItIndentFix.lua")
  LoadLuaFile("EdgeMode.lua")
  LoadLuaFile("SmartAutoCompleteHide.lua")
  LoadLuaFile("Tools.lua")
  LoadLuaFile("AutoItTools.lua")
  LoadLuaFile("AutoItGotoDefinition.lua")
  LoadLuaFile("AutoCloseBraces.lua")    -- Our Lua file
  
  -- Start up the events (Calls onstartup()).
  EventClass:BeginEvents()

At next copy the 'AutoCloseBraces.lua' into SciTE\LUA\

AutoCloseBraces = EventClass:new(Common)
  --------------------------------------------------------------------------------
  -- OnChar(charAdded)
  --
  -- AutoComplete Braces and Qoutes.
  --
  -- Parameters:
  --    charAdded - The character typed.
  --------------------------------------------------------------------------------
  
  function AutoCloseBraces:OnChar(charAdded)
  --    trace(charAdded)
      local toClose = { ['('] = ')', ['{'] = '}', ['['] = ']', ['"'] = '"', ["'"] = "'" }
  
      if toClose[charAdded] ~= nil then
          local pos = editor.CurrentPos
          editor:ReplaceSel(toClose[charAdded])
          editor:SetSel(pos, pos)
      end
      return false  -- Let next handler to process event
  --    return true  -- Don't let next handler to process event
  end

that's all, restart SciTE and now {, [, (, ' and " will be automtically closed and the cursor is set between.

Have Fun with this nice feature ... :)

Greetz

Greenhorn

Edited by Greenhorn
Link to comment
Share on other sites

  • Developers

You could improve that type of behaviour by testing what type the lexer identified and not add the closing character when you are inside a String or Comment for example. This is an example used in the LUA functions:

ls = editor.StyleAt[editor.CurrentPos]
if ls ~= SCE_AU3_COMMENTBLOCK and ls ~= SCE_AU3_COMMENT and ls ~= SCE_AU3_STRING and ls ~= SCE_AU3_PREPROCESSOR and ls ~= SCE_AU3_SPECIAL then

:)

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

You could improve that type of behaviour by testing what type the lexer identified and not add the closing character when you are inside a String or Comment for example. This is an example used in the LUA functions:

ls = editor.StyleAt[editor.CurrentPos]
  if ls ~= SCE_AU3_COMMENTBLOCK and ls ~= SCE_AU3_COMMENT and ls ~= SCE_AU3_STRING and ls ~= SCE_AU3_PREPROCESSOR and ls ~= SCE_AU3_SPECIAL then

:party:

I'm new to Lua, more precisely I took today the first time a deeper look into Lua and I don't really understand the syntax.

Maybe if I have more time I can try to learn Lua ... (C++ is in the moment in my brain and sucks it empty :lmao: )

[wish]

Oooor ... :) , maybe if you are bored for a while, you could add this to SciTE's menu as an optional feature ... :)

[/wish]

Greetz

Greenhorn

Edited by Greenhorn
Link to comment
Share on other sites

This will disable the feature in strings ...

--------------------------------------------------------------------------------
-- OnChar(charAdded)
--
-- AutoComplete Braces and Qoutes.
--
-- Parameters:
--  charAdded - The character typed.
--------------------------------------------------------------------------------

function AutoCloseBraces:OnChar(charAdded)
--  trace(charAdded)
    local toClose = { ['('] = ')', ['{'] = '}', ['['] = ']', ['"'] = '"', ["'"] = "'" }
    local ls = editor.StyleAt[editor.CurrentPos]

    if ls ~= SCE_AU3_STRING then
        if toClose[charAdded] ~= nil then
            local pos = editor.CurrentPos
            editor:ReplaceSel(toClose[charAdded])
            editor:SetSel(pos, pos)
        end
    end
    return false    -- Let next handler to process event
--  return true  -- Don't let next handler to process event
end
Link to comment
Share on other sites

I really like this feature; however, maybe a suggestion:

While inside of the ", ', (, [, groups, pressing enter would remove you from that. Having to constantly press the 'right' arrow key is quite frustrating.

So, when, for example, we are typing like this:

GuiCtrlCreateButton("this is a test"

If we were to press 'enter' after typing 'test', it should move us right after the ", same applies if it's one of the other groups too, thus allowing quicker code insertion.

Thanks :)

Edited by zackrspv

-_-------__--_-_-____---_-_--_-__-__-_ ^^€ñ†®øÞÿ ë×阮§ wï†høµ† ƒë@®, wï†høµ† †ïmë, @ñd wï†høµ† @ †ïmïdï†ÿ ƒø® !ïƒë. €×阮 ñø†, bµ† ïñ§†ë@d wï†hïñ, ñ@ÿ, †h®øµghøµ† †hë 맧ëñ§ë øƒ !ïƒë.

Link to comment
Share on other sites

This will disable the feature in strings ...

--------------------------------------------------------------------------------
-- OnChar(charAdded)
--
-- AutoComplete Braces and Qoutes.
--
-- Parameters:
--  charAdded - The character typed.
--------------------------------------------------------------------------------

function AutoCloseBraces:OnChar(charAdded)
--  trace(charAdded)
    local toClose = { ['('] = ')', ['{'] = '}', ['['] = ']', ['"'] = '"', ["'"] = "'" }
    local ls = editor.StyleAt[editor.CurrentPos]

    if ls ~= SCE_AU3_STRING then
        if toClose[charAdded] ~= nil then
            local pos = editor.CurrentPos
            editor:ReplaceSel(toClose[charAdded])
            editor:SetSel(pos, pos)
        end
    end
    return false    -- Let next handler to process event
--  return true  -- Don't let next handler to process event
end
Yours produces an error on startup.

-_-------__--_-_-____---_-_--_-__-__-_ ^^€ñ†®øÞÿ ë×阮§ wï†høµ† ƒë@®, wï†høµ† †ïmë, @ñd wï†høµ† @ †ïmïdï†ÿ ƒø® !ïƒë. €×阮 ñø†, bµ† ïñ§†ë@d wï†hïñ, ñ@ÿ, †h®øµghøµ† †hë 맧ëñ§ë øƒ !ïƒë.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...