Managing escaping when indenting
Another bug fix, phew, they do stack up when you really start using an application for real.
We’ve already managed double quotes when styling the code, this time we needed to handle them when indenting.
Up until now we’ve only checked one backslash, i.e. if a double quote has a backslash before it we disregard it, but what if that backslash has yet another backslash in front of it? Oops, didn’t think about that, or maybe I did but couldn’t be bothered at the time, well now I’ve found myself with “\\” strings and the indenting bugs out, so I suppose I can be bothered now.
In pico.rb:
def findParPos(sci, pos, direction, offset = 0)
lvl = 1
dq = 0
inc = direction == "right" ? 1 : -1
while(lvl != 0 && pos != sci.get_text_length)
case sci.get_char_at(pos)
when 34 # "
dq += 1 unless self.escaped?(sci, pos) # \
when 41
lvl -= inc if dq % 2 == 0
when 40
lvl += inc if dq % 2 == 0
end
break if pos == 0
pos = eval("sci." + self.getPosFunc(direction) + " " + pos.to_s)
end
pos + offset
end
So instead of simply checking for a single backslash we now use the method escaped? in lexer.rb:
def countSubsChrs(sci, start_chr, pos, dir)
cur_chr = start_chr
count = 0
while(pos > 0 || pos < sci.get_text_length)
pos += dir
break if cur_chr != (cur_chr = sci.get_char_at(pos))
count += 1
end
return count
end
def escaped?(sci, pos, dir = -1)
result = self.countSubsChrs(sci, 92, pos, dir) % 2
result == 0 ? false : true
end
92 is the ascii number for the backslash, hopefully this should work since we check for 0 or an even amount of backslashes. If we have an odd amount this should mean that the double quote in question is not escaped. It seems to be working, hopefully I won’t have to revisit this again 🙂