forked from siegebell/vsc-prettify-symbols-mode
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathposition.ts
72 lines (67 loc) · 3.01 KB
/
position.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import * as vscode from 'vscode';
//import {Substitution} from './configuration';
export function adjustCursorMovement(start: vscode.Position, end: vscode.Position, doc: vscode.TextDocument, avoidRanges: vscode.Range[]) : {pos: vscode.Position, range: vscode.Range} {
try {
const match = findInSortedRanges(end, avoidRanges, {excludeStart: true, excludeEnd: true});
if(match) {
if(start.line==end.line && start.character > end.character)
return {pos: match.range.start, range: match.range } // moving left
else if(start.line==end.line && start.character < end.character)
return {pos: match.range.end, range: match.range }; // moving right
}
} catch(e) {
console.log(e);
}
return {pos: end, range: undefined};
}
// function findClosestInPrettyDecorations(pos: vscode.Position, prettySubsts: PrettySubstitution[], options: {excludeStart?: boolean, excludeEnd?: boolean} = {excludeStart: false, excludeEnd: false}) {
// for(let prettyIdx = 0; prettyIdx < prettySubsts.length; ++prettyIdx) {
// const subst = prettySubsts[prettyIdx];
// let match = findClosestInSortedRanges(pos,subst.preRanges,options);
// if(match)
// return {range:match.range,index:match.index,prettyIndex:prettyIdx,pre: true};
// match = findClosestInSortedRanges(pos,subst.postRanges,options);
// if(match)
// return {range:match.range,index:match.index,prettyIndex:prettyIdx,pre:false};
// }
// return undefined;
// }
function findInSortedRanges(pos: vscode.Position, ranges: vscode.Range[], options: {excludeStart?: boolean, excludeEnd?: boolean} = {excludeStart: false, excludeEnd: false}) : {range:vscode.Range,index:number} {
const exclStart = options.excludeStart || false;
const exclEnd = options.excludeEnd || false;
let begin = 0;
let end = ranges.length;
while(begin < end) {
const idx = Math.floor((begin + end)/2);
const range = ranges[idx];
if(range.contains(pos) && !(exclStart && range.start.isEqual(pos)) && !(exclEnd && range.end.isEqual(pos)))
return {range: range, index: idx};
else if(pos.isBefore(range.start))
end = idx;
else
begin = idx+1;
}
return undefined;
}
/* function findClosestInSortedRanges(pos: vscode.Position, ranges: vscode.Range[], options: {excludeStart?: boolean, excludeEnd?: boolean} = {excludeStart: false, excludeEnd: false}) : {range:vscode.Range,index:number} {
const exclStart = options.excludeStart || false;
const exclEnd = options.excludeEnd || false;
let begin = 0;
let end = ranges.length;
while(begin < end) {
const idx = Math.floor((begin + end)/2);
const range = ranges[idx];
if(range.contains(pos) && !(exclStart && range.start.isEqual(pos)) && !(exclEnd && range.end.isEqual(pos)))
return {range: range, index: idx};
else if(pos.isBefore(range.start))
end = idx;
else
begin = idx+1;
}
for(let idx = begin; idx < ranges.length; ++idx) {
const range = ranges[idx];
if(range.start.isAfterOrEqual(pos) && !(exclStart && range.start.isEqual(pos)))
return {range: range, index: idx};
}
return undefined;
} */