forked from onivim/oni2
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
API - Range: Add 'explode' method (onivim#291)
* Factor range out of types * Refactor Range to separate module * Add interface * Formatting * Test cases for explode method
- Loading branch information
Showing
16 changed files
with
263 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
open Types; | ||
|
||
[@deriving show({with_path: false})] | ||
type t = { | ||
startPosition: Position.t, | ||
endPosition: Position.t, | ||
}; | ||
|
||
let createFromPositions = (~startPosition, ~endPosition, ()) => { | ||
startPosition, | ||
endPosition, | ||
}; | ||
|
||
let create = (~startLine, ~startCharacter, ~endLine, ~endCharacter, ()) => | ||
createFromPositions( | ||
~startPosition=Position.create(startLine, startCharacter), | ||
~endPosition=Position.create(endLine, endCharacter), | ||
(), | ||
); | ||
|
||
let zero = | ||
create( | ||
~startLine=ZeroBasedIndex(0), | ||
~startCharacter=ZeroBasedIndex(0), | ||
~endLine=ZeroBasedIndex(0), | ||
~endCharacter=ZeroBasedIndex(0), | ||
(), | ||
); | ||
|
||
let toZeroBasedPair = (v: Position.t) => { | ||
(Index.toZeroBasedInt(v.line), Index.toZeroBasedInt(v.character)); | ||
}; | ||
|
||
let explode = (measure, v) => { | ||
let (startLine, startCharacter) = v.startPosition |> toZeroBasedPair; | ||
let (endLine, endCharacter) = v.endPosition |> toZeroBasedPair; | ||
|
||
if (startLine == endLine) { | ||
[v]; | ||
} else { | ||
let idx = ref(startLine); | ||
|
||
let ranges = ref([]); | ||
|
||
while (idx^ < endLine) { | ||
let i = idx^; | ||
|
||
let startCharacter = i == startLine ? startCharacter : 0; | ||
let endCharacter = max(0, measure(i) - 1); | ||
|
||
ranges := | ||
[ | ||
create( | ||
~startLine=ZeroBasedIndex(i), | ||
~startCharacter=ZeroBasedIndex(startCharacter), | ||
~endCharacter=ZeroBasedIndex(endCharacter), | ||
~endLine=ZeroBasedIndex(i), | ||
(), | ||
), | ||
...ranges^, | ||
]; | ||
|
||
incr(idx); | ||
}; | ||
|
||
[ | ||
create( | ||
~startLine=ZeroBasedIndex(endLine), | ||
~startCharacter=ZeroBasedIndex(0), | ||
~endCharacter=ZeroBasedIndex(endCharacter), | ||
~endLine=ZeroBasedIndex(endLine), | ||
(), | ||
), | ||
...ranges^, | ||
] | ||
|> List.rev; | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/** | ||
* Range.rei | ||
* | ||
* Module dealing with Range (tuples of positions) | ||
*/ | ||
|
||
open Types; | ||
|
||
[@deriving show({with_path: false})] | ||
type t = { | ||
startPosition: Position.t, | ||
endPosition: Position.t, | ||
}; | ||
|
||
let createFromPositions: | ||
(~startPosition: Position.t, ~endPosition: Position.t, unit) => t; | ||
|
||
let create: | ||
( | ||
~startLine: Index.t, | ||
~startCharacter: Index.t, | ||
~endLine: Index.t, | ||
~endCharacter: Index.t, | ||
unit | ||
) => | ||
t; | ||
|
||
let zero: t; | ||
|
||
/* | ||
* explode(range, measure) takes a Range.t and a measurement function (int => int), | ||
* and expands a multiple-line range into a list of ranges, where there there is | ||
* a single range per-line. | ||
* | ||
* If the input range is a single line, a single item list with the input range | ||
* will be returned. | ||
*/ | ||
let explode: (int => int, t) => list(t); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
[@deriving show({with_path: false})] | ||
type mode = | ||
| None | ||
| Visual /* "v" */ | ||
| BlockwiseVisual /* "<C-v>" */ | ||
| LinewiseVisual; /* "V" */ | ||
|
||
[@deriving show({with_path: false})] | ||
type t = { | ||
range: Range.t, | ||
mode, | ||
}; | ||
|
||
let _modeFromString = s => { | ||
switch (s) { | ||
| "V" => LinewiseVisual | ||
| "vb" => BlockwiseVisual | ||
| "v" => Visual | ||
| _ => None | ||
}; | ||
}; | ||
|
||
/* | ||
* The range might not always come through in the correct 'order' - | ||
* this method normalizes the range so that the (startLine, startColumn) is | ||
* before or equal to (endLine, endColumn) | ||
*/ | ||
let _normalizeRange = (startLine, startColumn, endLine, endColumn) => | ||
if (startLine > endLine) { | ||
(endLine, endColumn, startLine, startColumn); | ||
} else if (startLine == endLine && startColumn > endColumn) { | ||
(endLine, endColumn, startLine, startColumn); | ||
} else { | ||
(startLine, startColumn, endLine, endColumn); | ||
}; | ||
|
||
let create = | ||
(~startLine=1, ~startColumn=1, ~endLine=1, ~endColumn=1, ~mode="", ()) => { | ||
let (startLine, startColumn, endLine, endColumn) = | ||
_normalizeRange(startLine, startColumn, endLine, endColumn); | ||
|
||
let range = | ||
Range.create( | ||
~startLine=OneBasedIndex(startLine), | ||
~startCharacter=OneBasedIndex(startColumn), | ||
~endLine=OneBasedIndex(endLine), | ||
~endCharacter=OneBasedIndex(endColumn), | ||
(), | ||
); | ||
|
||
let mode = _modeFromString(mode); | ||
|
||
{range, mode}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,6 @@ | |
*/ | ||
|
||
open Oni_Core; | ||
open Oni_Core.Types; | ||
|
||
module Diagnostic = { | ||
type t = {range: Range.t}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ | |
* or minimap. | ||
*/ | ||
|
||
open Oni_Core.Types; | ||
open Oni_Core; | ||
|
||
module Diagnostic: { | ||
type t = {range: Range.t}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.