Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
CodeMirror
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Issue analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Moritz Aurel Pascal Schubotz
CodeMirror
Commits
d46fd844
Commit
d46fd844
authored
10 years ago
by
Marijn Haverbeke
Browse files
Options
Downloads
Patches
Plain Diff
[sql-hint addon] Clean up, fix handling of whitespace
Closes #2761
parent
d0916042
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
addon/hint/sql-hint.js
+53
-57
53 additions, 57 deletions
addon/hint/sql-hint.js
mode/sql/index.html
+9
-1
9 additions, 1 deletion
mode/sql/index.html
with
62 additions
and
58 deletions
addon/hint/sql-hint.js
+
53
−
57
View file @
d46fd844
...
@@ -21,7 +21,7 @@
...
@@ -21,7 +21,7 @@
function
getKeywords
(
editor
)
{
function
getKeywords
(
editor
)
{
var
mode
=
editor
.
doc
.
modeOption
;
var
mode
=
editor
.
doc
.
modeOption
;
if
(
mode
===
"
sql
"
)
mode
=
"
text/x-sql
"
;
if
(
mode
===
"
sql
"
)
mode
=
"
text/x-sql
"
;
return
CodeMirror
.
resolveMode
(
mode
).
keywords
;
return
CodeMirror
.
resolveMode
(
mode
).
keywords
;
}
}
...
@@ -32,12 +32,12 @@
...
@@ -32,12 +32,12 @@
}
}
function
addMatches
(
result
,
search
,
wordlist
,
formatter
)
{
function
addMatches
(
result
,
search
,
wordlist
,
formatter
)
{
for
(
var
word
in
wordlist
)
{
for
(
var
word
in
wordlist
)
{
if
(
!
wordlist
.
hasOwnProperty
(
word
))
continue
;
if
(
!
wordlist
.
hasOwnProperty
(
word
))
continue
;
if
(
Array
.
isArray
(
wordlist
))
{
if
(
Array
.
isArray
(
wordlist
))
{
word
=
wordlist
[
word
];
word
=
wordlist
[
word
];
}
}
if
(
match
(
search
,
word
))
{
if
(
match
(
search
,
word
))
{
result
.
push
(
formatter
(
word
));
result
.
push
(
formatter
(
word
));
}
}
}
}
...
@@ -49,33 +49,30 @@
...
@@ -49,33 +49,30 @@
var
string
=
token
.
string
.
substr
(
1
);
var
string
=
token
.
string
.
substr
(
1
);
var
prevCur
=
Pos
(
cur
.
line
,
token
.
start
);
var
prevCur
=
Pos
(
cur
.
line
,
token
.
start
);
var
table
=
editor
.
getTokenAt
(
prevCur
).
string
;
var
table
=
editor
.
getTokenAt
(
prevCur
).
string
;
if
(
!
tables
.
hasOwnProperty
(
table
)
){
if
(
!
tables
.
hasOwnProperty
(
table
))
table
=
findTableByAlias
(
table
,
editor
);
table
=
findTableByAlias
(
table
,
editor
);
}
var
columns
=
tables
[
table
];
var
columns
=
tables
[
table
];
if
(
!
columns
)
{
if
(
!
columns
)
return
;
return
;
}
addMatches
(
result
,
string
,
columns
,
function
(
w
)
{
return
"
.
"
+
w
;});
addMatches
(
result
,
string
,
columns
,
function
(
w
)
{
return
"
.
"
+
w
;});
}
}
function
eachWord
(
lineText
,
f
)
{
function
eachWord
(
lineText
,
f
)
{
if
(
!
lineText
){
return
;
}
if
(
!
lineText
)
return
;
var
excepted
=
/
[
,;
]
/g
;
var
excepted
=
/
[
,;
]
/g
;
var
words
=
lineText
.
split
(
"
"
);
var
words
=
lineText
.
split
(
"
"
);
for
(
var
i
=
0
;
i
<
words
.
length
;
i
++
){
for
(
var
i
=
0
;
i
<
words
.
length
;
i
++
)
{
f
(
words
[
i
]?
words
[
i
].
replace
(
excepted
,
''
)
:
''
);
f
(
words
[
i
]?
words
[
i
].
replace
(
excepted
,
''
)
:
''
);
}
}
}
}
function
convertCurToNumber
(
cur
){
function
convertCurToNumber
(
cur
)
{
// max characters of a line is 999,999.
// max characters of a line is 999,999.
return
cur
.
line
+
cur
.
ch
/
Math
.
pow
(
10
,
6
);
return
cur
.
line
+
cur
.
ch
/
Math
.
pow
(
10
,
6
);
}
}
function
convertNumberToCur
(
num
){
function
convertNumberToCur
(
num
)
{
return
Pos
(
Math
.
floor
(
num
),
+
num
.
toString
().
split
(
'
.
'
).
pop
());
return
Pos
(
Math
.
floor
(
num
),
+
num
.
toString
().
split
(
'
.
'
).
pop
());
}
}
function
findTableByAlias
(
alias
,
editor
)
{
function
findTableByAlias
(
alias
,
editor
)
{
...
@@ -86,26 +83,26 @@
...
@@ -86,26 +83,26 @@
var
table
=
""
;
var
table
=
""
;
var
separator
=
[];
var
separator
=
[];
var
validRange
=
{
var
validRange
=
{
start
:
Pos
(
0
,
0
),
start
:
Pos
(
0
,
0
),
end
:
Pos
(
editor
.
lastLine
(),
editor
.
getLineHandle
(
editor
.
lastLine
()
).
length
)
end
:
Pos
(
editor
.
lastLine
(),
editor
.
getLineHandle
(
editor
.
lastLine
()).
length
)
};
};
//add separator
//add separator
var
indexOfSeparator
=
fullQuery
.
indexOf
(
CONS
.
QUERY_DIV
);
var
indexOfSeparator
=
fullQuery
.
indexOf
(
CONS
.
QUERY_DIV
);
while
(
indexOfSeparator
!=
-
1
){
while
(
indexOfSeparator
!=
-
1
)
{
separator
.
push
(
doc
.
posFromIndex
(
indexOfSeparator
));
separator
.
push
(
doc
.
posFromIndex
(
indexOfSeparator
));
indexOfSeparator
=
fullQuery
.
indexOf
(
CONS
.
QUERY_DIV
,
indexOfSeparator
+
1
);
indexOfSeparator
=
fullQuery
.
indexOf
(
CONS
.
QUERY_DIV
,
indexOfSeparator
+
1
);
}
}
separator
.
unshift
(
Pos
(
0
,
0
)
);
separator
.
unshift
(
Pos
(
0
,
0
)
);
separator
.
push
(
Pos
(
editor
.
lastLine
(),
editor
.
getLineHandle
(
editor
.
lastLine
()
).
text
.
length
)
);
separator
.
push
(
Pos
(
editor
.
lastLine
(),
editor
.
getLineHandle
(
editor
.
lastLine
()).
text
.
length
)
);
//find vali
eR
ange
//find vali
d r
ange
var
prevItem
=
0
;
var
prevItem
=
0
;
var
current
=
convertCurToNumber
(
editor
.
getCursor
()
);
var
current
=
convertCurToNumber
(
editor
.
getCursor
());
for
(
var
i
=
0
;
i
<
separator
.
length
;
i
++
){
for
(
var
i
=
0
;
i
<
separator
.
length
;
i
++
)
{
var
_v
=
convertCurToNumber
(
separator
[
i
]
);
var
_v
=
convertCurToNumber
(
separator
[
i
]);
if
(
current
>
prevItem
&&
current
<=
_v
){
if
(
current
>
prevItem
&&
current
<=
_v
)
{
validRange
=
{
start
:
convertNumberToCur
(
prevItem
),
end
:
convertNumberToCur
(
_v
)
};
validRange
=
{
start
:
convertNumberToCur
(
prevItem
),
end
:
convertNumberToCur
(
_v
)
};
break
;
break
;
}
}
prevItem
=
_v
;
prevItem
=
_v
;
...
@@ -113,52 +110,51 @@
...
@@ -113,52 +110,51 @@
var
query
=
doc
.
getRange
(
validRange
.
start
,
validRange
.
end
,
false
);
var
query
=
doc
.
getRange
(
validRange
.
start
,
validRange
.
end
,
false
);
for
(
var
i
=
0
;
i
<
query
.
length
;
i
++
){
for
(
var
i
=
0
;
i
<
query
.
length
;
i
++
)
{
var
lineText
=
query
[
i
];
var
lineText
=
query
[
i
];
eachWord
(
lineText
,
function
(
word
){
eachWord
(
lineText
,
function
(
word
)
{
var
wordUpperCase
=
word
.
toUpperCase
();
var
wordUpperCase
=
word
.
toUpperCase
();
if
(
wordUpperCase
===
aliasUpperCase
&&
tables
.
hasOwnProperty
(
previousWord
)
)
{
if
(
wordUpperCase
===
aliasUpperCase
&&
tables
.
hasOwnProperty
(
previousWord
)
)
{
table
=
previousWord
;
table
=
previousWord
;
}
}
if
(
wordUpperCase
!==
CONS
.
ALIAS_KEYWORD
){
if
(
wordUpperCase
!==
CONS
.
ALIAS_KEYWORD
)
{
previousWord
=
word
;
previousWord
=
word
;
}
}
});
});
if
(
table
){
break
;
}
if
(
table
)
break
;
}
}
return
table
;
return
table
;
}
}
function
sqlHint
(
editor
,
options
)
{
CodeMirror
.
registerHelper
(
"
hint
"
,
"
sql
"
,
function
(
editor
,
options
)
{
tables
=
(
options
&&
options
.
tables
)
||
{};
tables
=
(
options
&&
options
.
tables
)
||
{};
keywords
=
keywords
||
getKeywords
(
editor
);
keywords
=
keywords
||
getKeywords
(
editor
);
var
cur
=
editor
.
getCursor
();
var
cur
=
editor
.
getCursor
();
var
token
=
editor
.
getTokenAt
(
cur
),
end
=
token
.
end
;
var
result
=
[];
var
result
=
[];
var
search
=
token
.
string
.
trim
();
var
token
=
editor
.
getTokenAt
(
cur
),
start
,
end
,
search
;
if
(
token
.
string
.
match
(
/^
\.?[\w
@
]
+$/
))
{
search
=
token
.
string
;
start
=
token
.
start
;
end
=
token
.
end
;
}
else
{
start
=
end
=
cur
.
ch
;
search
=
""
;
}
if
(
search
.
charAt
(
0
)
==
"
.
"
)
{
if
(
search
.
charAt
(
0
)
==
"
.
"
)
{
columnCompletion
(
result
,
editor
);
columnCompletion
(
result
,
editor
);
if
(
!
result
.
length
)
{
if
(
!
result
.
length
)
{
while
(
token
.
start
&&
search
.
charAt
(
0
)
==
"
.
"
)
{
while
(
start
&&
search
.
charAt
(
0
)
==
"
.
"
)
{
token
=
editor
.
getTokenAt
(
Pos
(
cur
.
line
,
token
.
start
-
1
));
token
=
editor
.
getTokenAt
(
Pos
(
cur
.
line
,
token
.
start
-
1
));
start
=
token
.
start
;
search
=
token
.
string
+
search
;
search
=
token
.
string
+
search
;
}
}
addMatches
(
result
,
search
,
tables
,
addMatches
(
result
,
search
,
tables
,
function
(
w
)
{
return
w
;});
function
(
w
)
{
return
w
;});
}
}
}
else
{
}
else
{
addMatches
(
result
,
search
,
keywords
,
addMatches
(
result
,
search
,
tables
,
function
(
w
)
{
return
w
;});
function
(
w
)
{
return
w
.
toUpperCase
();});
addMatches
(
result
,
search
,
keywords
,
function
(
w
)
{
return
w
.
toUpperCase
();});
addMatches
(
result
,
search
,
tables
,
function
(
w
)
{
return
w
;});
}
}
return
{
return
{
list
:
result
,
from
:
Pos
(
cur
.
line
,
start
),
to
:
Pos
(
cur
.
line
,
end
)};
list
:
result
,
});
from
:
Pos
(
cur
.
line
,
token
.
start
),
to
:
Pos
(
cur
.
line
,
end
)
};
}
CodeMirror
.
registerHelper
(
"
hint
"
,
"
sql
"
,
sqlHint
);
});
});
This diff is collapsed.
Click to expand it.
mode/sql/index.html
+
9
−
1
View file @
d46fd844
...
@@ -7,6 +7,9 @@
...
@@ -7,6 +7,9 @@
<link
rel=
"stylesheet"
href=
"../../lib/codemirror.css"
/>
<link
rel=
"stylesheet"
href=
"../../lib/codemirror.css"
/>
<script
src=
"../../lib/codemirror.js"
></script>
<script
src=
"../../lib/codemirror.js"
></script>
<script
src=
"sql.js"
></script>
<script
src=
"sql.js"
></script>
<link
rel=
"stylesheet"
href=
"../../addon/hint/show-hint.css"
/>
<script
src=
"../../addon/hint/show-hint.js"
></script>
<script
src=
"../../addon/hint/sql-hint.js"
></script>
<style>
<style>
.CodeMirror
{
.CodeMirror
{
border-top
:
1px
solid
black
;
border-top
:
1px
solid
black
;
...
@@ -68,7 +71,12 @@ window.onload = function() {
...
@@ -68,7 +71,12 @@ window.onload = function() {
smartIndent
:
true
,
smartIndent
:
true
,
lineNumbers
:
true
,
lineNumbers
:
true
,
matchBrackets
:
true
,
matchBrackets
:
true
,
autofocus
:
true
autofocus
:
true
,
extraKeys
:
{
"
Ctrl-Space
"
:
"
autocomplete
"
},
hintOptions
:
{
tables
:
{
users
:
{
name
:
null
,
score
:
null
,
birthDate
:
null
},
countries
:
{
name
:
null
,
population
:
null
,
size
:
null
}
}}
});
});
};
};
</script>
</script>
...
...
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