Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Indentation issue
Started by krystian6 Feb 09 2014 01:17 AM

30 replies to this topic
[TOPIC CONTROLS]
Page 1 of 2 1 2
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

krystian6

[GLOBAL: userInfoPane.html]
krystian6
  • Contributor

  • 560 posts
  • Corona SDK

Hello,

 

although on a daily basis I use a real IDE for work, I decided to give another try to Corona Editor, especially since it now supports debugging I've been asking for in one of my discussions.

Anyway... Apart from work I work on some new ideas and test stuff and I use Corona Editor for it now, but I'm struggling with something most basic... code indentation.

 

When I reformat my code, there are two issues I have to manually fix: formatting of 'elseif' (everywhere) and 'end' (of an anonymous function passed to onComplete/timer/event).

 

Here are two samples:

transition.to(signal, {alpha = 0, xScale = 2, yScale = 2, time = 400, transition = easing.inOutQuad, onComplete = function(target) 
        target:removeSelf()
        end})
 

 

 

if event.target == boardTop then
    spots = topSpots
    elseif event.target == boardBottom then
        spots = bottomSpots
        else
            assert(false, "unknown board")
        end

 

I'm not that fluent with Sublime to fix it myself. I've looked at the Lua code template, and the increaseIndentPattern looks ok.

 

Any ideas on how to fix this?

 

 

 

EDIT:

Ok... now that I wrote all of this, I have found the decreaseIndentPattern option, and it seems it lacks handling of this situation. I'm not sure it won't screw things up in other cases, but now reformatting my file works as expected. I've added .* at the end of the pattern.

 

 

<string>^\s*(elseif|else|end|\}).*\s*$</string>

BTW.

If you want your code formatting to work like in IntelliJ IDEA, you can add this to your user's keybinding:

 

 

{ "keys": ["command+alt+l"], "command": "reindent", "args": {"single_line": false} }

 

Happy coding!

 

 

EDIT2:

 

And to all of you who prefer syntax:

 

local table = {
    blah = 2
}

over:

 

local table =
{
    blah = 2
}

here's add .* into your increaseIndentPattern before the \{

 

 

<string>^\s*(else|elseif|for|(local\s+)?function|if|repeat|until|while)\b((?!end).)*$|.*\{\s*$</string>


[TOPIC: post.html]
#2

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 25,378 posts
  • Enterprise

What files are those located in?  This is very useful.  The unindents frustrate the bajeebies for me.

 

Rob



[TOPIC: post.html]
#3

krystian6

[GLOBAL: userInfoPane.html]
krystian6
  • Contributor

  • 560 posts
  • Corona SDK

Rob,

 

I've edited

 

Library/Application Support/Sublime Text 2/Packages/Lua/Lua.tmLanguage
 

Unfortunately I didn't get the 

 

 

local whatever = function()
    print("blah")
end
 

 

 

to work yet. Let me know if you get the right pattern.

 

Thanks

Krystian



[TOPIC: post.html]
#4

ricardo.valerio

[GLOBAL: userInfoPane.html]
ricardo.valerio
  • Observer

  • 1 posts
  • Corona SDK

Rob,

 

I've edited

Library/Application Support/Sublime Text 2/Packages/Lua/Lua.tmLanguage
 

Unfortunately I didn't get the 

local whatever = function()
    print("blah")
end
 

to work yet. Let me know if you get the right pattern.

 

Thanks

Krystian

 

A simple fix would go along the lines of:

    <key>increaseIndentPattern</key>
    <string>^\s*(else|elseif|for|(local\s+)?function|if|repeat|until|while)\b((?!end).)*$|.*=\s*function\s*\(\)\s*$|.*\{\s*$</string>

Where we are just basically adding the ".*=\s*function\s*\(\)\s*$". Didn't go through all scenarios, but I think it works ok for regular cases.

 

Edit:

 

Upon reviewing this, I certainly forgot functions with parameters :-) Ops! I suppose something like .*=\s*function\s*\([^)]*\)\s*$ could work, but later I will test and provide a more correct answer.



[TOPIC: post.html]
#5

Perry Clarke

[GLOBAL: userInfoPane.html]
Perry Clarke
  • Moderator

  • 850 posts
  • Corona Staff

Thanks for the report.  I've filed an issue.



[TOPIC: post.html]
#6

landoncope

[GLOBAL: userInfoPane.html]
landoncope
  • Contributor

  • 112 posts
  • Corona SDK

Anyone know a fix for this for ST 3? A guy here seems to have solved it for ST 2: http://blog.lotech.org/2013/04/fixing-lua-indenting-in-sublime-text.html



[TOPIC: post.html]
#7

landoncope

[GLOBAL: userInfoPane.html]
landoncope
  • Contributor

  • 112 posts
  • Corona SDK

Found a better approach. See my later comment

 

I figured out a fix for both the elseif and function(args) indention issues (for ST 3 on OSX).

 

This worked for me:

  1. Navigate to Applications/Sublime Text.app (show package contents)/Contents/MacOS/Packages/
  2. Rename Lua.sublime-package to Lua.zip and unzip
  3. Replace code in Indent.tmPreferences with code below
  4. Compress back to .zip and rename back to Lua.sublime-package
  5. Restart Sublime
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>name</key>
  <string>Indent</string>
  <key>scope</key>
  <string>source.lua</string>
  <key>settings</key>
  <dict>
    <key>decreaseIndentPattern</key>
    <string>^\s*(else|end|\})\s*$|^\s*elseif\b((?!end).)*$</string>
    <key>increaseIndentPattern</key>
    <string>^\s*(else|elseif|for|(local\s+)?function|if|repeat|until|while)\b((?!end).)*$|.*=\s*function\s*\([^)]*\)\s*$|.*\{\s*$</string>
  </dict>
  <key>uuid</key>
  <string>411468A8-E0AC-415A-9E71-E2BD091EB571</string>
</dict>
</plist>

As I think about this, these settings will probably get overwritten the next time Sublime issues an update. You can also try creating a Lua folder in your Browse Packages directory, and copying the modified Indent.tmPreferences into it. You may also need the Lua.tmLanguage file. I tried testing this approach, but Sublime appears to be doing some package caching that I don't quite understand. Let me know if this works for you.



[TOPIC: post.html]
#8

mroberti

[GLOBAL: userInfoPane.html]
mroberti
  • Contributor

  • 308 posts
  • Corona SDK

Hey this almost works for Windows Sublime too!! Still get issues with indentation after a line of commented code. But your other changes work great. Still unusable for me, since...y'know I comment code. ;)

 

Before reindent:

function Cleanup()
-- First, delete previous trail
print("Before cleanup size of table "..#scrollers)
if(#scrollers>0)then
for i = #scrollers,1,-1 do
local child = scrollers[i]
if(child~=nil)then
display.remove( child )
child = nil
end
end
scrollers = nil
scrollers = {}
end
 
print("Size of table after cleanup "..#scrollers)
 
end

 

And after going to EDIT->LINE->REINDENT

 

function Cleanup()
-- First, delete previous trail
print("Before cleanup size of table "..#scrollers)
if(#scrollers>0)then
    for i = #scrollers,1,-1 do
        local child = scrollers[i]
        if(child~=nil)then
            display.remove( child )
            child = nil
        end
    end
    scrollers = nil
    scrollers = {}
end
 
print("Size of table after cleanup "..#scrollers)
 
end


Damn, SOOOOOOO close. This glitch is the ONLY thing keeping me from going full Sublime. 

 

Appreciate your efforts to kill this bug!!! 

 

-Mario



[TOPIC: post.html]
#9

landoncope

[GLOBAL: userInfoPane.html]
landoncope
  • Contributor

  • 112 posts
  • Corona SDK

Found a better approach. See my later comment

 

mroberti-

 

I was able to fix that as well. Do the following (close Sublime first for this one):

  1. Navigate to Applications/Sublime Text.app (show package contents)/Contents/MacOS/Packages/
  2. Rename Default.sublime-package to Default.zip and unzip
  3. Replace code in Indentation Rules - Comments.tmPreferences with code below
  4. Compress back to zip and rename back to Default.sublime-package
  5. Reopen Sublime
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>scope</key>
    <string>comment</string>
    <key>settings</key>
    <dict>
        <key>preserveIndent</key>
        <false/>
    </dict>
</dict>
</plist>


[TOPIC: post.html]
#10

mroberti

[GLOBAL: userInfoPane.html]
mroberti
  • Contributor

  • 308 posts
  • Corona SDK

YESSSSSSSSSS!!!!!!! It worked!! I'm on Windows, BTW but I found the packages under:

 

C:\Program Files\Sublime Text 3\Packages

 

In case any other Windows folks want to fix this.

 

THANK YOU THANK YOU THANK YOU!!!!!!

 

-Mario



[TOPIC: post.html]
#11

landoncope

[GLOBAL: userInfoPane.html]
landoncope
  • Contributor

  • 112 posts
  • Corona SDK

These changes will probably revert when Sublime updates, but Perry should be able to implement them into his CoronaSDK package.



[TOPIC: post.html]
#12

Perry Clarke

[GLOBAL: userInfoPane.html]
Perry Clarke
  • Moderator

  • 850 posts
  • Corona Staff

I plan too.  This is partly why the Corona SDK Lua syntax definition exists, because updating the Lua syntax will get overwritten.  If you prefer to use the Lua syntax file make sure you report the issues so there's some chance they'll be fixed (I believe the Sublime Text forums are right place to do this though I seem to recall some effort to start using Github for the "packages" and I'm not sure if syntax definitions fall under that).  Note that the Corona SDK Lua syntax definition colorizes most of the Corona SDK specific stuff as well as the regular Lua things.



[TOPIC: post.html]
#13

landoncope

[GLOBAL: userInfoPane.html]
landoncope
  • Contributor

  • 112 posts
  • Corona SDK

Perry, I am using the Corona SDK Lua syntax, which I much prefer. The changes I described above affect both the Lua and Corona SDK Lua syntaxes, I'm guessing because you haven't specified any .tmPreferences in your package (just a guess.. I've only been using Sublime a few days now).



[TOPIC: post.html]
#14

landoncope

[GLOBAL: userInfoPane.html]
landoncope
  • Contributor

  • 112 posts
  • Corona SDK

I found that with my above approach, changing the Default.sublime-package was resulting in shortcuts for Toggle Comment, Toggle Block Comment, and Duplicate Line being disabled (because of hidden files OSX includes in zip which are difficult to remove). So here is a better approach which will also persist between Sublime updates.

 

*Note that if you have the shortcut issue mentioned above, delete the Sublime Text.app and reinstall. Your user settings and plugins will persist.

 

  1. Install PackageResourceViewer from command palette
  2. Run "PackageResourceViewer: Open Resource" from command palette
  3. Navigate to Lua/Indent.tmPreferences and change middle block to look like this:
        <key>decreaseIndentPattern</key>
        <string>^\s*(else|else\s*--.*|end|end\s*--.*|\}\s*,|\})\s*$|^\s*elseif\b((?!end).)*$</string>
        <key>increaseIndentPattern</key>
        <string>^\s*(else|elseif|for|(local\s+)?function|if|repeat|until|while)\b((?!end).)*$|.*=\s*function\s*\([^)]*\)\s*$|.*\{\s*$</string>
  4. Next use "PackageResourceViewer: Open Resource" to open Default/Rules - Comments.tmPreferences and change middle block to look like this:
        <dict>
            <key>preserveIndent</key>
            <false/>
        </dict>
  5. Make sure you save those 2 files (the plugin creates a copy of them in the appropriate places)
  6. This step may be unnecessary, but clean the contents of Cache folder (for me, found at Library/Application Support/Sublime Text 3/Cache)


[TOPIC: post.html]
#15

mroberti

[GLOBAL: userInfoPane.html]
mroberti
  • Contributor

  • 308 posts
  • Corona SDK

So this works great except the "Run" (Windows+F10) command fails to launch the simulator, however the "Debug" option works (F10) after erroring out on the first line, then hitting F10 again to "step through" the first false positive error....I don't debug much so this workaround is OK. Hell, it's closer than I was a month ago with Sublime!!

 

Thanks again landoncope, this is really glorious!!!

 

-Mario



[TOPIC: post.html]
#16

landoncope

[GLOBAL: userInfoPane.html]
landoncope
  • Contributor

  • 112 posts
  • Corona SDK

You'll have to give some more details of what happens when you try to "Run" your project, including console output. 



[TOPIC: post.html]
#17

mroberti

[GLOBAL: userInfoPane.html]
mroberti
  • Contributor

  • 308 posts
  • Corona SDK

Hey!

 

Well, console shows normal output as if it's executing:

Corona Editor: Init
Package Control: Skipping automatic upgrade, last run at 2015-07-26 14:54:01, next run at 2015-07-26 15:54:01 or after
Corona Editor: Running: ['C:\\Program Files (x86)\\Corona Labs\\Corona SDK\\Corona Simulator.exe', '/singleton', '/no-console', 'C:\\Users\\mroberti\\Desktop\\SWN\\main.lua']

Then, it just sits there. I check TaskManager and I don't see an instance of Corona fired up. 

I grabbed the edited pacakges I changed according to your directions above, copy them somewhere, uninstall Sublime and reinstall it, then re-copy my pacakages back in and still get the same results. 



[TOPIC: post.html]
#18

landoncope

[GLOBAL: userInfoPane.html]
landoncope
  • Contributor

  • 112 posts
  • Corona SDK

 

 

Corona Editor: Running: ['C:\\Program Files (x86)\\Corona Labs\\Corona SDK\\Corona Simulator.exe'

 

Is that path accurate? On OSX, the daily build path differs from the standard Corona path. Also, what file is active in Sublime when you try to run it? You may run into some issues if the active .lua file is in a subdirectory relative to main.lua. 

 

I'm sad that the Corona plugin hasn't been touched at all this year. It appears to have a number of problems. I've considered going in and fixing some of these issues myself, but if it's dead I don't want to bother. 



[TOPIC: post.html]
#19

mroberti

[GLOBAL: userInfoPane.html]
mroberti
  • Contributor

  • 308 posts
  • Corona SDK

Yeah the path is correct. What's damned peculiar is all the changes we're applying don't mess with any paths...maybe shortcut issues? But yeah I'm with ya. I'm sad about the editor. I've said it before and I'll say it again, back to LUA Glider...

 

-Mario



[TOPIC: post.html]
#20

landoncope

[GLOBAL: userInfoPane.html]
landoncope
  • Contributor

  • 112 posts
  • Corona SDK

I love Lua Glider, except for how slow it is (Netbeans being Java based). And sadly, its development has been dead for at least a year.



[TOPIC: post.html]
#21

landoncope

[GLOBAL: userInfoPane.html]
landoncope
  • Contributor

  • 112 posts
  • Corona SDK

And yeah, the changes above shouldn't have any effect on running your projects.. Are you able to run your projects without the changes?



[TOPIC: post.html]
#22

mroberti

[GLOBAL: userInfoPane.html]
mroberti
  • Contributor

  • 308 posts
  • Corona SDK

Yeah without changes on a fresh reinstall of Sublime it runs fine.

 

re: LUA Glider, Ditto. The slowness has really started to bum me out lately. But, it indents without issue. Sublime really IS spunky and responsive....but yeah. Dang. 

 

-Mario



[TOPIC: post.html]
#23

landoncope

[GLOBAL: userInfoPane.html]
landoncope
  • Contributor

  • 112 posts
  • Corona SDK

If you delete the Default folder in your Sublime user packages directory, and the cache folder and restart Sublime, are you able to run projects? The are probably in something like "C:\Users\{user}\AppData\Roaming\Sublime Text 3\" on Windows.



[TOPIC: post.html]
#24

mroberti

[GLOBAL: userInfoPane.html]
mroberti
  • Contributor

  • 308 posts
  • Corona SDK

I don't see anything named "default" or "cache folder" or whatever to delete in that directory to delete. 



[TOPIC: post.html]
#25

landoncope

[GLOBAL: userInfoPane.html]
landoncope
  • Contributor

  • 112 posts
  • Corona SDK

In Sublime, go to Preferences->Browse Packages. You should see a Default folder here, and the Cache folder one level above it. 




[topic_controls]
Page 1 of 2 1 2
 
[/topic_controls]