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
5d22f9c3
Commit
5d22f9c3
authored
12 years ago
by
Yunchi Luo
Committed by
Marijn Haverbeke
12 years ago
Browse files
Options
Downloads
Patches
Plain Diff
[vim keymap] More unit tests and fixes. Most keys covered now.
parent
017dad45
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
keymap/vim.js
+46
-23
46 additions, 23 deletions
keymap/vim.js
test/vim_test.js
+126
-0
126 additions, 0 deletions
test/vim_test.js
with
172 additions
and
23 deletions
keymap/vim.js
+
46
−
23
View file @
5d22f9c3
...
...
@@ -73,6 +73,8 @@
{
keys
:
[
'
Ctrl-p
'
],
type
:
'
keyToKey
'
,
toKeys
:
[
'
k
'
]
},
{
keys
:
[
'
Ctrl-[
'
],
type
:
'
keyToKey
'
,
toKeys
:
[
'
Esc
'
]
},
{
keys
:
[
'
Ctrl-c
'
],
type
:
'
keyToKey
'
,
toKeys
:
[
'
Esc
'
]
},
{
keys
:
[
'
s
'
],
type
:
'
keyToKey
'
,
toKeys
:
[
'
c
'
,
'
l
'
]
},
{
keys
:
[
'
S
'
],
type
:
'
keyToKey
'
,
toKeys
:
[
'
c
'
,
'
c
'
]
},
// Motions
{
keys
:
[
'
h
'
],
type
:
'
motion
'
,
motion
:
'
moveByCharacters
'
,
...
...
@@ -178,13 +180,6 @@
{
keys
:
[
'
i
'
],
type
:
'
action
'
,
action
:
'
enterInsertMode
'
},
{
keys
:
[
'
I
'
],
type
:
'
action
'
,
action
:
'
enterInsertMode
'
,
motion
:
'
moveToFirstNonWhiteSpaceCharacter
'
},
{
keys
:
[
'
s
'
],
type
:
'
action
'
,
action
:
'
enterInsertMode
'
,
motion
:
'
moveByCharacters
'
,
motionArgs
:
{
forward
:
true
},
operator
:
'
delete
'
},
{
keys
:
[
'
S
'
],
type
:
'
action
'
,
action
:
'
enterInsertMode
'
,
motion
:
'
moveByLines
'
,
motionArgs
:
{
forward
:
true
,
linewise
:
true
,
explicitRepeat
:
true
},
operator
:
'
delete
'
},
{
keys
:
[
'
o
'
],
type
:
'
action
'
,
action
:
'
newLineAndEnterInsertMode
'
,
actionArgs
:
{
after
:
true
}},
{
keys
:
[
'
O
'
],
type
:
'
action
'
,
action
:
'
newLineAndEnterInsertMode
'
,
...
...
@@ -592,6 +587,7 @@
actionArgs
.
repeatIsExplicit
=
repeatIsExplicit
;
actionArgs
.
registerName
=
inputState
.
registerName
;
inputState
.
reset
();
vim
.
lastMotion
=
null
,
actions
[
command
.
action
](
cm
,
actionArgs
,
vim
);
},
evalInput
:
function
(
cm
,
vim
)
{
...
...
@@ -993,9 +989,19 @@
var
curEnd
=
{
line
:
lineNumEnd
,
ch
:
lineLength
(
cm
,
lineNumEnd
)
-
1
};
}
var
text
=
cm
.
getRange
(
curStart
,
curEnd
).
replace
(
/
\n\s
*/g
,
'
'
);
cm
.
replaceRange
(
text
,
curStart
,
curEnd
);
cm
.
setCursor
(
curStart
);
var
finalCh
=
0
;
cm
.
operation
(
function
()
{
for
(
var
i
=
curStart
.
line
;
i
<
curEnd
.
line
;
i
++
)
{
finalCh
=
lineLength
(
cm
,
curStart
.
line
);
var
tmp
=
{
line
:
curStart
.
line
+
1
,
ch
:
lineLength
(
cm
,
curStart
.
line
+
1
)
};
var
text
=
cm
.
getRange
(
curStart
,
tmp
);
text
=
text
.
replace
(
/
\n\s
*/g
,
'
'
);
cm
.
replaceRange
(
text
,
curStart
,
tmp
);
}
var
curFinalPos
=
{
line
:
curStart
.
line
,
ch
:
finalCh
};
cm
.
setCursor
(
curFinalPos
);
});
},
newLineAndEnterInsertMode
:
function
(
cm
,
actionArgs
)
{
var
insertAt
=
cm
.
getCursor
();
...
...
@@ -1013,27 +1019,37 @@
for
(
var
text
=
''
,
i
=
0
;
i
<
actionArgs
.
repeat
;
i
++
)
{
text
+=
register
.
text
;
}
var
curChEnd
=
0
;
var
linewise
=
register
.
linewise
;
if
(
linewise
)
{
cur
.
line
+=
actionArgs
.
after
?
1
:
0
;
cur
.
ch
=
0
;
curChEnd
=
0
;
if
(
actionArgs
.
after
)
{
// Move the newline at the end to the start instead, and paste just
// before the newline character of the line we are on right now.
text
=
'
\n
'
+
text
.
slice
(
0
,
text
.
length
-
1
);
cur
.
ch
=
lineLength
(
cm
,
cur
.
line
);
}
else
{
cur
.
ch
=
0
;
}
}
else
{
cur
.
ch
+=
actionArgs
.
after
?
1
:
0
;
curChEnd
=
cur
.
ch
;
}
// Set cursor in the right place to let CodeMirror handle moving it.
cm
.
setCursor
(
cur
.
line
,
curChEnd
);
cm
.
replaceRange
(
text
,
cur
);
// Now fine tune the cursor to where we want it.
if
(
linewise
)
{
cm
.
setCursor
(
cm
.
getCursor
().
line
-
1
,
0
);
}
else
{
cur
=
cm
.
getCursor
();
cm
.
setCursor
(
cur
.
line
,
cur
.
ch
-
1
);
var
curPosFinal
;
var
idx
;
if
(
linewise
&&
actionArgs
.
after
)
{
curPosFinal
=
makeCursor
(
cur
.
line
+
1
,
findFirstNonWhiteSpaceCharacter
(
cm
.
getLine
(
cur
.
line
+
1
)));
}
else
if
(
linewise
&&
!
actionArgs
.
after
)
{
curPosFinal
=
makeCursor
(
cur
.
line
,
findFirstNonWhiteSpaceCharacter
(
cm
.
getLine
(
cur
.
line
)));
}
else
if
(
!
linewise
&&
actionArgs
.
after
)
{
idx
=
cm
.
indexFromPos
(
cur
);
curPosFinal
=
cm
.
posFromIndex
(
idx
+
text
.
length
-
1
);
}
else
{
idx
=
cm
.
indexFromPos
(
cur
);
curPosFinal
=
cm
.
posFromIndex
(
idx
+
text
.
length
);
}
cm
.
setCursor
(
curPosFinal
);
},
undo
:
function
(
cm
,
actionArgs
)
{
repeatFn
(
cm
,
CodeMirror
.
commands
.
undo
,
actionArgs
.
repeat
)();
...
...
@@ -1068,6 +1084,7 @@
replaceWithStr
+=
replaceWith
;
}
cm
.
replaceRange
(
replaceWithStr
,
curStart
,
curEnd
);
cm
.
setCursor
(
offsetCursor
(
curEnd
,
0
,
-
1
));
}
};
...
...
@@ -1118,6 +1135,12 @@
}
return
ret
;
}
function
makeCursor
(
line
,
ch
)
{
return
{
line
:
line
,
ch
:
ch
};
};
function
offsetCursor
(
cur
,
offsetLine
,
offsetCh
)
{
return
{
line
:
cur
.
line
+
offsetLine
,
ch
:
cur
.
ch
+
offsetCh
};
};
function
arrayEq
(
a1
,
a2
)
{
if
(
a1
.
length
!=
a2
.
length
)
return
false
;
for
(
var
i
=
0
;
i
<
a1
.
length
;
i
++
)
{
...
...
This diff is collapsed.
Click to expand it.
test/vim_test.js
+
126
−
0
View file @
5d22f9c3
...
...
@@ -212,6 +212,56 @@ testMotion('%_squares', ['%'], squares1.end, squares1.start);
testMotion
(
'
%_braces
'
,
[
'
%
'
],
curlys1
.
end
,
curlys1
.
start
);
// Operator tests
testVim
(
'
dl
'
,
function
(
cm
,
vim
,
helpers
)
{
var
curStart
=
makeCursor
(
0
,
0
);
cm
.
setCursor
(
curStart
);
helpers
.
doKeys
(
'
d
'
,
'
l
'
);
eq
(
'
word1
'
,
cm
.
getValue
());
var
register
=
vim
.
registerController
.
getRegister
();
eq
(
'
'
,
register
.
text
);
is
(
!
register
.
linewise
);
eqPos
(
curStart
,
cm
.
getCursor
());
},
{
value
:
'
word1
'
});
testVim
(
'
dl_repeat
'
,
function
(
cm
,
vim
,
helpers
)
{
var
curStart
=
makeCursor
(
0
,
0
);
cm
.
setCursor
(
curStart
);
helpers
.
doKeys
(
'
2
'
,
'
d
'
,
'
l
'
);
eq
(
'
ord1
'
,
cm
.
getValue
());
var
register
=
vim
.
registerController
.
getRegister
();
eq
(
'
w
'
,
register
.
text
);
is
(
!
register
.
linewise
);
eqPos
(
curStart
,
cm
.
getCursor
());
},
{
value
:
'
word1
'
});
testVim
(
'
dh
'
,
function
(
cm
,
vim
,
helpers
)
{
var
curStart
=
makeCursor
(
0
,
3
);
cm
.
setCursor
(
curStart
);
helpers
.
doKeys
(
'
d
'
,
'
h
'
);
eq
(
'
wrd1
'
,
cm
.
getValue
());
var
register
=
vim
.
registerController
.
getRegister
();
eq
(
'
o
'
,
register
.
text
);
is
(
!
register
.
linewise
);
eqPos
(
offsetCursor
(
curStart
,
0
,
-
1
),
cm
.
getCursor
());
},
{
value
:
'
word1
'
});
testVim
(
'
dj
'
,
function
(
cm
,
vim
,
helpers
)
{
var
curStart
=
makeCursor
(
0
,
3
);
cm
.
setCursor
(
curStart
);
helpers
.
doKeys
(
'
d
'
,
'
j
'
);
eq
(
'
word3
'
,
cm
.
getValue
());
var
register
=
vim
.
registerController
.
getRegister
();
eq
(
'
word1
\n
word2
\n
'
,
register
.
text
);
is
(
register
.
linewise
);
eqPos
(
makeCursor
(
0
,
1
),
cm
.
getCursor
());
},
{
value
:
'
word1
\n
word2
\n
word3
'
});
testVim
(
'
dk
'
,
function
(
cm
,
vim
,
helpers
)
{
var
curStart
=
makeCursor
(
1
,
3
);
cm
.
setCursor
(
curStart
);
helpers
.
doKeys
(
'
d
'
,
'
k
'
);
eq
(
'
word3
'
,
cm
.
getValue
());
var
register
=
vim
.
registerController
.
getRegister
();
eq
(
'
word1
\n
word2
\n
'
,
register
.
text
);
is
(
register
.
linewise
);
eqPos
(
makeCursor
(
0
,
1
),
cm
.
getCursor
());
},
{
value
:
'
word1
\n
word2
\n
word3
'
});
testVim
(
'
dw_space
'
,
function
(
cm
,
vim
,
helpers
)
{
var
curStart
=
makeCursor
(
0
,
0
);
cm
.
setCursor
(
curStart
);
...
...
@@ -489,3 +539,79 @@ testVim('I', function(cm, vim, helpers) {
eqPos
(
makeCursor
(
0
,
lines
[
0
].
textStart
),
cm
.
getCursor
());
eq
(
'
vim-insert
'
,
cm
.
getOption
(
'
keyMap
'
));
});
testVim
(
'
o
'
,
function
(
cm
,
vim
,
helpers
)
{
cm
.
setCursor
(
0
,
4
);
helpers
.
doKeys
(
'
o
'
);
eq
(
'
word1
\n\n
word2
'
,
cm
.
getValue
());
eqPos
(
makeCursor
(
1
,
0
),
cm
.
getCursor
());
eq
(
'
vim-insert
'
,
cm
.
getOption
(
'
keyMap
'
));
},
{
value
:
'
word1
\n
word2
'
});
testVim
(
'
O
'
,
function
(
cm
,
vim
,
helpers
)
{
cm
.
setCursor
(
0
,
4
);
helpers
.
doKeys
(
'
O
'
);
eq
(
'
\n
word1
\n
word2
'
,
cm
.
getValue
());
eqPos
(
makeCursor
(
0
,
0
),
cm
.
getCursor
());
eq
(
'
vim-insert
'
,
cm
.
getOption
(
'
keyMap
'
));
},
{
value
:
'
word1
\n
word2
'
});
testVim
(
'
J
'
,
function
(
cm
,
vim
,
helpers
)
{
cm
.
setCursor
(
0
,
4
);
helpers
.
doKeys
(
'
J
'
);
var
expectedValue
=
'
word1 word2
\n
word3
\n
word4
'
;
eq
(
expectedValue
,
cm
.
getValue
());
eqPos
(
makeCursor
(
0
,
expectedValue
.
indexOf
(
'
word2
'
)
-
1
),
cm
.
getCursor
());
},
{
value
:
'
word1
\n
word2
\n
word3
\n
word4
'
});
testVim
(
'
J_repeat
'
,
function
(
cm
,
vim
,
helpers
)
{
cm
.
setCursor
(
0
,
4
);
helpers
.
doKeys
(
'
3
'
,
'
J
'
);
var
expectedValue
=
'
word1 word2 word3
\n
word4
'
;
eq
(
expectedValue
,
cm
.
getValue
());
eqPos
(
makeCursor
(
0
,
expectedValue
.
indexOf
(
'
word3
'
)
-
1
),
cm
.
getCursor
());
},
{
value
:
'
word1
\n
word2
\n
word3
\n
word4
'
});
testVim
(
'
p
'
,
function
(
cm
,
vim
,
helpers
)
{
cm
.
setCursor
(
0
,
1
);
vim
.
registerController
.
pushText
(
'
"
'
,
'
yank
'
,
'
abc
\n
def
'
,
false
);
helpers
.
doKeys
(
'
p
'
);
eq
(
'
__abc
\n
def_
'
,
cm
.
getValue
());
eqPos
(
makeCursor
(
1
,
2
),
cm
.
getCursor
());
},
{
value
:
'
___
'
});
testVim
(
'
p_register
'
,
function
(
cm
,
vim
,
helpers
)
{
cm
.
setCursor
(
0
,
1
);
vim
.
registerController
.
getRegister
(
'
a
'
).
set
(
'
abc
\n
def
'
,
false
);
helpers
.
doKeys
(
'
"
'
,
'
a
'
,
'
p
'
);
eq
(
'
__abc
\n
def_
'
,
cm
.
getValue
());
eqPos
(
makeCursor
(
1
,
2
),
cm
.
getCursor
());
},
{
value
:
'
___
'
});
testVim
(
'
p_wrong_register
'
,
function
(
cm
,
vim
,
helpers
)
{
cm
.
setCursor
(
0
,
1
);
vim
.
registerController
.
getRegister
(
'
a
'
).
set
(
'
abc
\n
def
'
,
false
);
helpers
.
doKeys
(
'
p
'
);
eq
(
'
___
'
,
cm
.
getValue
());
eqPos
(
makeCursor
(
0
,
1
),
cm
.
getCursor
());
},
{
value
:
'
___
'
});
testVim
(
'
p_line
'
,
function
(
cm
,
vim
,
helpers
)
{
cm
.
setCursor
(
0
,
1
);
vim
.
registerController
.
pushText
(
'
"
'
,
'
yank
'
,
'
a
\n
d
\n
'
,
true
);
helpers
.
doKeys
(
'
2
'
,
'
p
'
);
eq
(
'
___
\n
a
\n
d
\n
a
\n
d
'
,
cm
.
getValue
());
eqPos
(
makeCursor
(
1
,
2
),
cm
.
getCursor
());
},
{
value
:
'
___
'
});
testVim
(
'
P
'
,
function
(
cm
,
vim
,
helpers
)
{
cm
.
setCursor
(
0
,
1
);
vim
.
registerController
.
pushText
(
'
"
'
,
'
yank
'
,
'
abc
\n
def
'
,
false
);
helpers
.
doKeys
(
'
P
'
);
eq
(
'
_abc
\n
def__
'
,
cm
.
getValue
());
eqPos
(
makeCursor
(
1
,
3
),
cm
.
getCursor
());
},
{
value
:
'
___
'
});
testVim
(
'
P_line
'
,
function
(
cm
,
vim
,
helpers
)
{
cm
.
setCursor
(
0
,
1
);
vim
.
registerController
.
pushText
(
'
"
'
,
'
yank
'
,
'
a
\n
d
\n
'
,
true
);
helpers
.
doKeys
(
'
2
'
,
'
P
'
);
eq
(
'
a
\n
d
\n
a
\n
d
\n
___
'
,
cm
.
getValue
());
eqPos
(
makeCursor
(
0
,
2
),
cm
.
getCursor
());
},
{
value
:
'
___
'
});
testVim
(
'
r
'
,
function
(
cm
,
vim
,
helpers
)
{
cm
.
setCursor
(
0
,
1
);
helpers
.
doKeys
(
'
3
'
,
'
r
'
,
'
u
'
);
eq
(
'
wuuuet
'
,
cm
.
getValue
());
eqPos
(
makeCursor
(
0
,
3
),
cm
.
getCursor
());
},
{
value
:
'
wordet
'
});
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