Skip to content

Commit

Permalink
a few fixes in impl of Expand Selection
Browse files Browse the repository at this point in the history
  • Loading branch information
ngn committed Oct 23, 2015
1 parent c61d032 commit 0c07cd2
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions client/keymap.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,24 +52,24 @@ $.extend(CodeMirror.commands,{
},
EXP:function(cm){
var sels=cm.listSelections()
if(sels.length!==1){
var ll=cm.lastLine(),u=cm.getCursor(),l=u.line
if(sels.length===1){
var ll=cm.lastLine(), u=cm.getCursor(), l=u.line
function cmp(x,y){return(x[0]-y[0])||(x[1]-y[1])} // compare two pairs of numbers
var ranges=[ // candidates for selection
[[l,0],[l, cm.getLine(l) .length]] // current line
[[0,0],[ll,cm.getLine(ll).length]] // whole document
[[l,0],[l, cm.getLine(l) .length]], // current line
[[0,0],[ll,cm.getLine(ll).length]] // whole document
]
var t0=cm.getTokenAt(u),t1=cm.getTokenAt({line:u.line,ch:u.ch+1}),t=t0||t1
var t0=cm.getTokenAt(u), t1=cm.getTokenAt({line:u.line,ch:u.ch+1}), t=t0||t1
if(t0&&t1&&(t0.start!==t1.start||t0.end!==t1.end)){ // we must decide which token looks more important
var lr=[t0,t1].map(function(ti){return(ti.type||'').replace(/^.*\bapl-(\w*)$/,'$1')}) // lft and rgt token type
var I={'var':5,glb:5, quad:4, str:3, num:2, kw:1, par:-1,sqbr:-1,semi:-1,dfn:-1, '':-2} // importance table
t=(I[lr[0]]||0)>(I[lr[1]]||0)?t0:t1 // t is the more important of t0 t1
}
t&&ranges.push[[l,t.start],[l,t.end]] // current token
t&&ranges.push([[l,t.start],[l,t.end]]) // current token
ranges.sort(function(x,y){return cmp(y[0],x[0])||cmp(x[1],y[1])}) // inside-out order: desc beginnings, then asc ends
var sel=sels[0],s=[[sel.anchor.line,sel.anchor.ch],[sel.head.line,sel.head.ch]].sort(cmp)
var sel=sels[0], s=[[sel.anchor.line,sel.anchor.ch],[sel.head.line,sel.head.ch]].sort(cmp), Pos=CodeMirror.Pos
for(var i=0;i<ranges.length;i++){
var r=ranges[i],d0=cmp(r[0],s[0]),d1=cmp(r[1],s[1]),Pos=CodeMirror.Pos
var r=ranges[i], d0=cmp(r[0],s[0]), d1=cmp(r[1],s[1])
if(d0<=0&&0<=d1&&(d0||d1)){cm.setSelection(Pos(r[0][0],r[0][1]),Pos(r[1][0],r[1][1]));break}
}
}
Expand Down

0 comments on commit 0c07cd2

Please sign in to comment.