Compare commits

...

5 Commits

Author SHA1 Message Date
Scott Morrison
f3a41ad2fe add to doc-string 2024-02-08 09:15:15 +11:00
Scott Morrison
dc27ad8926 remove findIndentAndIsStart 2024-02-08 09:12:04 +11:00
Scott Morrison
0918bff25c fix namespace 2024-02-06 22:06:06 +11:00
Scott Morrison
a50e6b4c23 Update src/Init/Data/String/Basic.lean
Co-authored-by: David Thrane Christiansen <david@davidchristiansen.dk>
2024-02-06 21:26:45 +11:00
Scott Morrison
ebbfa1a522 feat: upstream some Syntax/Position helper functions used in code actions in Std 2024-02-06 20:29:15 +11:00
3 changed files with 30 additions and 0 deletions

View File

@@ -515,6 +515,12 @@ def replace (s pattern replacement : String) : String :=
termination_by s.endPos.1 - pos.1
loop "" 0 0
/-- Return the beginning of the line that contains character `pos`. -/
def findLineStart (s : String) (pos : String.Pos) : String.Pos :=
match s.revFindAux (· = '\n') pos with
| none => 0
| some n => n.byteIdx + 1
end String
namespace Substring

View File

@@ -84,6 +84,26 @@ partial def toPosition (fmap : FileMap) (pos : String.Pos) : Position :=
-- Can also happen with EOF errors, which are not strictly inside the file.
lines.back, (pos - ps.back).byteIdx
/-- Convert a `Lean.Position` to a `String.Pos`. -/
def ofPosition (text : FileMap) (pos : Position) : String.Pos :=
let colPos :=
if h : pos.line - 1 < text.positions.size then
text.positions.get pos.line - 1, h
else if text.positions.isEmpty then
0
else
text.positions.back
String.Iterator.nextn text.source, colPos pos.column |>.pos
/--
Returns the position of the start of (1-based) line `line`.
This gives the stame result as `map.ofPosition ⟨line, 0⟩`, but is more efficient.
-/
def lineStart (map : FileMap) (line : Nat) : String.Pos :=
if h : line - 1 < map.positions.size then
map.positions.get line - 1, h
else map.positions.back?.getD 0
end FileMap
end Lean

View File

@@ -305,6 +305,10 @@ def getRange? (stx : Syntax) (canonicalOnly := false) : Option String.Range :=
| some start, some stop => some { start, stop }
| _, _ => none
/-- Returns a synthetic Syntax which has the specified `String.Range`. -/
def ofRange (range : String.Range) (canonical := true) : Lean.Syntax :=
.atom (.synthetic range.start range.stop canonical) ""
/--
Represents a cursor into a syntax tree that can be read, written, and advanced down/up/left/right.
Indices are allowed to be out-of-bound, in which case `cur` is `Syntax.missing`.