good morning!!!!
Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
B
bor
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Harbor Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
open
bor
Commits
dbcdf83e
Commit
dbcdf83e
authored
8 years ago
by
Bas van Kervel
Browse files
Options
Downloads
Patches
Plain Diff
console: ignore round and curly brackets in strings when determining indentation level
parent
fdd61b83
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
console/console.go
+45
-2
45 additions, 2 deletions
console/console.go
console/console_test.go
+46
-0
46 additions, 0 deletions
console/console_test.go
with
91 additions
and
2 deletions
console/console.go
+
45
−
2
View file @
dbcdf83e
...
...
@@ -330,11 +330,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
{
...
...
@@ -353,6 +353,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
)
...
...
This diff is collapsed.
Click to expand it.
console/console_test.go
+
46
−
0
View file @
dbcdf83e
...
...
@@ -294,3 +294,49 @@ func TestPrettyError(t *testing.T) {
t
.
Fatalf
(
"pretty error mismatch: have %s, want %s"
,
output
,
want
)
}
}
// Tests that tests if the number of indents for JS input is calculated correct.
func
TestIndenting
(
t
*
testing
.
T
)
{
testCases
:=
[]
struct
{
input
string
expectedIndentCount
int
}{
{
`var a = 1;`
,
0
},
{
`"some string"`
,
0
},
{
`"some string with (parentesis`
,
0
},
{
`"some string with newline
("`
,
0
},
{
`function v(a,b) {}`
,
0
},
{
`function f(a,b) { var str = "asd("; };`
,
0
},
{
`function f(a) {`
,
1
},
{
`function f(a, function(b) {`
,
2
},
{
`function f(a, function(b) {
var str = "a)}";
});`
,
0
},
{
`function f(a,b) {
var str = "a{b(" + a, ", " + b;
}`
,
0
},
{
`var str = "\"{"`
,
0
},
{
`var str = "'("`
,
0
},
{
`var str = "\\{"`
,
0
},
{
`var str = "\\\\{"`
,
0
},
{
`var str = 'a"{`
,
0
},
{
`var obj = {`
,
1
},
{
`var obj = { {a:1`
,
2
},
{
`var obj = { {a:1}`
,
1
},
{
`var obj = { {a:1}, b:2}`
,
0
},
{
`var obj = {}`
,
0
},
{
`var obj = {
a: 1, b: 2
}`
,
0
},
{
`var test = }`
,
-
1
},
{
`var str = "a\""; var obj = {`
,
1
},
}
for
i
,
tt
:=
range
testCases
{
counted
:=
countIndents
(
tt
.
input
)
if
counted
!=
tt
.
expectedIndentCount
{
t
.
Errorf
(
"test %d: invalid indenting: have %d, want %d"
,
i
,
counted
,
tt
.
expectedIndentCount
)
}
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment