diff options
author | Bas van Kervel <bas@ethdev.com> | 2016-06-08 18:17:38 +0800 |
---|---|---|
committer | Péter Szilágyi <peterke@gmail.com> | 2016-06-14 22:11:54 +0800 |
commit | 71b577f83953838d79cf78b77ab0d9d671bd5edf (patch) | |
tree | e2759fb081836e37343a72d385e483ccdbfc11b5 /console/console.go | |
parent | a93d63d5767cca58dd14bce7f659de099ee561c0 (diff) | |
download | go-tangerine-71b577f83953838d79cf78b77ab0d9d671bd5edf.tar go-tangerine-71b577f83953838d79cf78b77ab0d9d671bd5edf.tar.gz go-tangerine-71b577f83953838d79cf78b77ab0d9d671bd5edf.tar.bz2 go-tangerine-71b577f83953838d79cf78b77ab0d9d671bd5edf.tar.lz go-tangerine-71b577f83953838d79cf78b77ab0d9d671bd5edf.tar.xz go-tangerine-71b577f83953838d79cf78b77ab0d9d671bd5edf.tar.zst go-tangerine-71b577f83953838d79cf78b77ab0d9d671bd5edf.zip |
[release/1.4.7] console: ignore round and curly brackets in strings when determining indentation level
(cherry picked from commit dbcdf83ed8aca3f0b84d67b944fff2f3a8bc7769)
Diffstat (limited to 'console/console.go')
-rw-r--r-- | console/console.go | 47 |
1 files changed, 45 insertions, 2 deletions
diff --git a/console/console.go b/console/console.go index ab0c1ea58..00d1fea1d 100644 --- a/console/console.go +++ b/console/console.go @@ -331,11 +331,11 @@ func (c *Console) Interactive() { // Append the line to the input and check for multi-line interpretation input += line + "\n" - indents = strings.Count(input, "{") + strings.Count(input, "(") - strings.Count(input, "}") - strings.Count(input, ")") + indents = countIndents(input) if indents <= 0 { prompt = c.prompt } else { - prompt = strings.Repeat("..", indents*2) + " " + prompt = strings.Repeat(".", indents*3) + " " } // If all the needed lines are present, save the command and run if indents <= 0 { @@ -354,6 +354,49 @@ func (c *Console) Interactive() { } } +// countIndents returns the number of identations for the given input. +// In case of invalid input such as var a = } the result can be negative. +func countIndents(input string) int { + var ( + indents = 0 + inString = false + strOpenChar = ' ' // keep track of the string open char to allow var str = "I'm ...."; + charEscaped = false // keep track if the previous char was the '\' char, allow var str = "abc\"def"; + ) + + for _, c := range input { + switch c { + case '\\': + // indicate next char as escaped when in string and previous char isn't escaping this backslash + if !charEscaped && inString { + charEscaped = true + } + case '\'', '"': + if inString && !charEscaped && strOpenChar == c { // end string + inString = false + } else if !inString && !charEscaped { // begin string + inString = true + strOpenChar = c + } + charEscaped = false + case '{', '(': + if !inString { // ignore brackets when in string, allow var str = "a{"; without indenting + indents++ + } + charEscaped = false + case '}', ')': + if !inString { + indents-- + } + charEscaped = false + default: + charEscaped = false + } + } + + return indents +} + // Execute runs the JavaScript file specified as the argument. func (c *Console) Execute(path string) error { return c.jsre.Exec(path) |