Sublime Forum

Help with indenting

#1

I created a language indent rules file, but my indenting is only mostly working, so should I just drop all the details in here or contact someone for support?

TIA
Mikey

0 Likes

#2

drop the details here, we’ll try to help you :slight_smile:

0 Likes

#3

Total beginner at ST, and horrible at regex, so please no laughing.
Repo at https://github.com/macMikey/livecode-sublimetext
A few examples of things that don’t work:

  1. Lines that begin with “#” break indenting of everything below them
  2. else, else if, end if don’t back-indent
  3. Indenting follow-on structures doesn’t happen. For example, if I have a “block” (line begins with #<), and the following line is the opening of a repeat structure, the repeat will indent, but the code inside of the repeat will not, then the end repeat will not unindent
  4. If a line is an if-then, and the following line is an else or else if indenting breaks

Here’s the file:

    <?xml version="1.0" encoding="UTF-8"?>
    <plist version="1.0">
    <dict>
    	<key>name</key>
    	<string>LiveCode Script Indentation</string>
    	<key>scope</key>
    	<string>source.livecode</string>
    	<key>settings</key>
    	<!-- documenation found at http://docs.sublimetext.info/en/latest/reference/metadata.html?highlight=indentation -->
    	<dict>
    		<key>increaseIndentPattern</key>
    			<string>^\s*?((on\s+?.+)|((private\s)?((command|function)\s+?.+))|((else?\s*)?(if\s+?.+then\s*?)((#|--).*?)?$)|(else\s*?((#|--).*?)?$)|(repeat\s+?.+)|(switch\s?.+)|(case\s+?.+)|(default.*)|(try\s*?.*)|(catch\s+?.+)|(finally\s?.*)|((#|--|)\s*?&lt;\s*?[^/].*?&gt;))</string>
    		<key>decreaseIndentPattern</key>
    			<string>^\s*((end\s+?.+)|(case\s+?.+)|(default\s*?.*)|(else.*)|(catch\s+?.+)|(finally\s*?.*)|((#|--|)\s*?&lt;/.*?&gt;))</string>
    		<key>disableIndentNextLinePattern</key>
    			<!-- rules for when then next line would not be indented further -->
    			<string>^\s*?(if\s+?.+?then\s+?.+)</string>
    		<key>bracketIndentNextLinePattern</key>
    			<!-- if current line matches then next line (and only next line) indented 1 level further -->
    			<string></string>
    		<key>unIndentedLinePattern</key>
    			<!-- ignore any lines matching this pattern when computing the next line's indent level -->
    			<string></string>
    	</dict>
    </dict>
    </plist>
0 Likes

#4

Can you edit your post to make the XML of your patterns plain text so that it comes across properly in the forum? That will make it easier to check out what you’ve got so far.

To do that you can select all of the XML and then press the editor button that looks like </> to tell the forum that it should treat that text as plain text, or edit you can it so that the first and last lines contain three backticks all on one line, which would look something like this in the post editor:

```
<xml version="1.0" encoding="UTF-8">
<plist version="1.0">

...

</plist>
```
0 Likes

#5

LiveCode seems quite hard to get the indentation rules correct for within the limitations of the TextMate indentation engine that ST uses. That said, you could try this:

LiveCode-UnindentIf.tmPreferences:

<?xml version="1.0" encoding="UTF-8"?>
<plist version="1.0">
<dict>
	<key>name</key>
	<string>LiveCode Script If Block Unindentation</string>
	<key>scope</key>
	<string>source.livecode meta.if.block.livecode</string>
	<key>settings</key>
	<!-- documenation found at http://docs.sublimetext.info/en/latest/reference/metadata.html?highlight=indentation -->
	<dict>
		<key>decreaseIndentPattern</key>
		<string>^\s*(?:else|end\s+if)\b</string>
	</dict>
</dict>
</plist>

LiveCode-Indent.tmPreferences:

<?xml version="1.0" encoding="UTF-8"?>
<plist version="1.0">
<dict>
	<key>name</key>
	<string>LiveCode Script Indentation</string>
	<key>scope</key>
	<string>source.livecode</string>
	<key>settings</key>
	<!-- documenation found at http://docs.sublimetext.info/en/latest/reference/metadata.html?highlight=indentation -->
	<dict>
		<key>increaseIndentPattern</key>
		<string><![CDATA[(?x)
			(?<comment>\s*(?:\#|--|/\*)){0}
			(?<comment_or_eol>\g<comment>|\s*\\?$){0}
			^\s*
			(
				(on\s)
			|	(private\s+)?(command|function)\s+
			|	(else\g<comment_or_eol>)
			|	(else\s+)?
				(if\s+.+?)?
				\bthen\g<comment_or_eol>
			|	repeat\s
			|	switch\s
			|	case\s
			|	default\b
			|	try\b
			|	catch\b
			|	finally\b
			)
		]]></string>
		<key>decreaseIndentPattern</key>
		<string>(?x)
			^\s*
			(
				end\s+.
			|	case\s+.
			|	default\b
			|	catch\b
			|	finally\b
			)
		</string>
		<key>unIndentedLinePattern</key>
		<!-- ignore any lines matching this pattern when computing the next line's indent level -->
		<string>^\s*(?:#|--|/\*)</string><!-- i.e. any comments -->
	</dict>
</dict>
</plist>
0 Likes

#6

First, TIA for the help. Since you guys have so graciously offered to help, I’m going to switch from mangler of indent rules to testing robot.

First problem:

#<test>
	#</test>

The second line should unindent. So would I create another prefs file for that, like the unindentif file?
I’ll keep documenting issues as I find them.

0 Likes

#7

Next one: If I insert a construct that should cause everything below it to indent, only the next line indents.

The code might start as

repeat for each line theLine in theList
   put "prefix" before theLine
   put "suffix" after theLine
end repeat

Then I add a block comment on front

#<walk the list>

What I want is

#<walk the list>
   repeat for each line theLine in theList
      put "prefix" before theLine
      put "suffix" after theLine
   end repeat

What I get is

#<walk the list>
   repeat for each line theLine in theList #this line indents
      put "prefix" before theLine 
      put "suffix" after theLine 
end repeat #this line should indent
0 Likes

#8

Does anyone have any other suggestions on what I can do? TIA.

0 Likes

#9

After fiddling, I’m on to the next question. Sorry, regEx is hard to read and write. There was a mistake in the proposed new indent file. Now that I have that fixed (fixed one is below) I would like comment lines that are of the form #<…> to signify the beginning of a block, which I want to indent, and #</…> to specify the end of said block (where “…” is a random string)
Thus

#<this is a block>
   So I want the contents inside the block to indent
#</this is a block>

If I’m reading the regex properly, we are excluding all comments from indenting, right?

Would it be simpler/easier/cheaper/faster/better to modify the livecode.sublime-syntax file and the symbol list.tmpreferences files? Someone else wrote those, so I’m green as grass on trying to read them.

Here’s the corrected indent file before we add blocks:

<?xml version="1.0" encoding="UTF-8"?>
<plist version="1.0">
<dict>
	<key>name</key>
	<string>LiveCode Script Indentation</string>
	<key>scope</key>
	<string>source.livecode</string>
	<key>settings</key>
	<!-- documenation found at http://docs.sublimetext.info/en/latest/reference/metadata.html?highlight=indentation -->
	<dict>
		<key>increaseIndentPattern</key>
		<string><![CDATA[(?x)
			(?<comment>\s*(?:\#|--|/\*)){0}
			(?<eol_or_comment>\g<comment>|\s*\\?$){0}
			^\s*
			(
				(on\s)
			|	(private\s+)?(command|function)\s+
			|	(else\g<eol_or_comment>)
			|	(else\s+)?
				(if\s+.+?)?
				\bthen\g<eol_or_comment>
			|	repeat\s
			|	switch\s
			|	case\s
			|	default\b
			|	try\b
			|	catch\b
			|	finally\b
			)
		]]></string>
		<key>decreaseIndentPattern</key>
		<string>(?x)
			^\s*
			(
				end\s+.
			|	case\s+.
			|	default\b
			|	catch\b
			|	finally\b
			)
		</string>
		<key>unIndentedLinePattern</key>
		<!-- ignore any lines matching this pattern when computing the next line's indent level -->
		<string>^\s*(?:#|--|/\*)</string><!-- i.e. any comments -->
	</dict>
</dict>
</plist>
0 Likes

#10

I’ve been fighting with this some more and I’m still no closer, so I would really appreciate the help.
I’m trying to make a special kind of comment that indents the enclosed code. The opening of the block will be a hashtagged html tag, e.g.

#<this is a block>

and the closing of the block will similarly be

#</this is a block>

I’ve been reading up on regex and trying to make something work, but ST does not like the syntax, even syntax that I check using online regex testers.

0 Likes

#11

Regex can be confusing and it looks plain ugly when trying to do certain things… If you’re new with it - one of the best ways to get something done with regards to RegEx is to look at all of the other language files - tmLanguage and sublime-syntax… Determine which ones are needed from those examples ( and there are hundreds of examples with just the default languages - most of which could be ready to use or need a minor tweak to work for you )…

I’d suggest starting there…

Also, you seem to use a lot of spaces. The default examples don’t - and I can’t recall whether or not a whitespace is literal outside of [] char matching / set… but it could be looking for all of those spaces you add from ( … (on\s) and the new lines… I’d suggest getting rid of spaces which aren’t needed ( unless someone can give a definite answer in relation to this )…

I use WordWrap enabled when messing in the default language files - it may not show up as nicely as your spaced RegEx but theirs works and you’re having issues - start with HTML and XML then look at PHP / ASP because of the start and close tags…

Make a backup of your file and remove the spaces and see if that changes anything too…

0 Likes

#12

The only .tmpreferences file in this thread that is mine is the top one. The others are Keith’s invention. As for the spaces, I’m not sure how to answer that. For example, in LC, the beginnning of a handler is “on”, some number of spaces, then the name of the handler. The end of the same handler is “end”, some number of spaces, then the name of the handler.

0 Likes