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
6b831b83
Commit
6b831b83
authored
8 years ago
by
Marijn Haverbeke
Browse files
Options
Downloads
Patches
Plain Diff
[indent-fold addon] Ignore comment lines
Closes #4548
parent
13aed570
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/fold/indent-fold.js
+19
-15
19 additions, 15 deletions
addon/fold/indent-fold.js
demo/folding.html
+29
-0
29 additions, 0 deletions
demo/folding.html
with
48 additions
and
15 deletions
addon/fold/indent-fold.js
+
19
−
15
View file @
6b831b83
...
...
@@ -11,32 +11,36 @@
})(
function
(
CodeMirror
)
{
"
use strict
"
;
function
lineIndent
(
cm
,
lineNo
)
{
var
text
=
cm
.
getLine
(
lineNo
)
var
spaceTo
=
text
.
search
(
/
\S
/
)
if
(
spaceTo
==
-
1
||
/
\b
comment
\b
/
.
test
(
cm
.
getTokenTypeAt
(
CodeMirror
.
Pos
(
lineNo
,
spaceTo
+
1
))))
return
-
1
return
CodeMirror
.
countColumn
(
text
,
null
,
cm
.
getOption
(
"
tabSize
"
))
}
!
CodeMirror
.
registerHelper
(
"
fold
"
,
"
indent
"
,
function
(
cm
,
start
)
{
var
tabSize
=
cm
.
getOption
(
"
tabSize
"
),
firstLine
=
cm
.
getLine
(
start
.
line
);
if
(
!
/
\S
/
.
test
(
firstLine
))
return
;
var
getIndent
=
function
(
line
)
{
return
CodeMirror
.
countColumn
(
line
,
null
,
tabSize
);
};
var
myIndent
=
getIndent
(
firstLine
);
var
lastLineInFold
=
null
;
var
myIndent
=
lineIndent
(
cm
,
start
.
line
)
if
(
myIndent
<
0
)
return
var
lastLineInFold
=
null
// Go through lines until we find a line that definitely doesn't belong in
// the block we're folding, or to the end.
for
(
var
i
=
start
.
line
+
1
,
end
=
cm
.
lastLine
();
i
<=
end
;
++
i
)
{
var
curLine
=
cm
.
getLine
(
i
)
;
var
curIndent
=
getIndent
(
curLine
);
if
(
curI
ndent
>
myIndent
)
{
var
indent
=
lineIndent
(
cm
,
i
)
if
(
indent
==
-
1
)
{
}
else
if
(
i
ndent
>
myIndent
)
{
// Lines with a greater indent are considered part of the block.
lastLineInFold
=
i
;
}
else
if
(
!
/
\S
/
.
test
(
curLine
))
{
// Empty lines might be breaks within the block we're trying to fold.
}
else
{
// A non-empty line at an indent equal to or less than ours marks the
// start of another block.
// If this line has non-space, non-comment content, and is
// indented less or equal to the start line, it is the start of
// another block.
break
;
}
}
if
(
lastLineInFold
)
return
{
from
:
CodeMirror
.
Pos
(
start
.
line
,
firstL
ine
.
length
),
from
:
CodeMirror
.
Pos
(
start
.
line
,
cm
.
getLine
(
start
.
l
ine
)
.
length
),
to
:
CodeMirror
.
Pos
(
lastLineInFold
,
cm
.
getLine
(
lastLineInFold
).
length
)
};
});
...
...
This diff is collapsed.
Click to expand it.
demo/folding.html
+
29
−
0
View file @
6b831b83
...
...
@@ -12,10 +12,12 @@
<script
src=
"../addon/fold/foldgutter.js"
></script>
<script
src=
"../addon/fold/brace-fold.js"
></script>
<script
src=
"../addon/fold/xml-fold.js"
></script>
<script
src=
"../addon/fold/indent-fold.js"
></script>
<script
src=
"../addon/fold/markdown-fold.js"
></script>
<script
src=
"../addon/fold/comment-fold.js"
></script>
<script
src=
"../mode/javascript/javascript.js"
></script>
<script
src=
"../mode/xml/xml.js"
></script>
<script
src=
"../mode/python/python.js"
></script>
<script
src=
"../mode/markdown/markdown.js"
></script>
<style
type=
"text/css"
>
.CodeMirror
{
border-top
:
1px
solid
black
;
border-bottom
:
1px
solid
black
;}
...
...
@@ -43,6 +45,24 @@
<textarea
id=
"code"
name=
"code"
></textarea></div>
<div
style=
"max-width: 50em; margin-bottom: 1em"
>
HTML:
<br>
<textarea
id=
"code-html"
name=
"code-html"
></textarea></div>
<div
style=
"max-width: 50em"
>
Python:
<br>
<textarea
id=
"code-python"
name=
"code"
>
def foo():
do_some_stuff()
here
return None
class Bar:
__init__(self):
if True:
print("True")
else:
print("False")
this_code_makes_no_sense():
pass
# A comment
</textarea></div>
<div
style=
"max-width: 50em"
>
Markdown:
<br>
<textarea
id=
"code-markdown"
name=
"code"
></textarea></div>
</form>
...
...
@@ -57,6 +77,7 @@ window.onload = function() {
sc
.
innerHTML
=
""
;
var
te_html
=
document
.
getElementById
(
"
code-html
"
);
te_html
.
value
=
document
.
documentElement
.
innerHTML
;
var
te_python
=
document
.
getElementById
(
"
code-python
"
);
var
te_markdown
=
document
.
getElementById
(
"
code-markdown
"
);
te_markdown
.
value
=
"
# Foo
\n
## Bar
\n\n
blah blah
\n\n
## Baz
\n\n
blah blah
\n\n
# Quux
\n\n
blah blah
\n
"
...
...
@@ -81,6 +102,14 @@ window.onload = function() {
editor_html
.
foldCode
(
CodeMirror
.
Pos
(
0
,
0
));
editor_html
.
foldCode
(
CodeMirror
.
Pos
(
21
,
0
));
window
.
editor_python
=
CodeMirror
.
fromTextArea
(
te_python
,
{
mode
:
"
python
"
,
lineNumbers
:
true
,
extraKeys
:
{
"
Ctrl-Q
"
:
function
(
cm
){
cm
.
foldCode
(
cm
.
getCursor
());
}},
foldGutter
:
true
,
gutters
:
[
"
CodeMirror-linenumbers
"
,
"
CodeMirror-foldgutter
"
]
});
window
.
editor_markdown
=
CodeMirror
.
fromTextArea
(
te_markdown
,
{
mode
:
"
markdown
"
,
lineNumbers
:
true
,
...
...
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