Skip to content
This repository was archived by the owner on Aug 24, 2024. It is now read-only.

Commit 17da0fa

Browse files
committed
feat: combine DocStringTokenInfo and TypeTokenInfo
1 parent b1f57bd commit 17da0fa

File tree

2 files changed

+38
-56
lines changed

2 files changed

+38
-56
lines changed

LeanInk/Analysis/DataTypes.lean

Lines changed: 27 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -44,38 +44,26 @@ instance : Positional Fragment where
4444

4545
/- Token -/
4646
/--
47-
`TokenInfo` is the base structure of all `Token`.
48-
Inherently it describe the semantic info of the token, which can and should be used for semantic syntax
49-
highlighting.
47+
`SemanticTokenInfo` describe the semantic info of the token, which can and should be used for semantic syntax highlighting.
5048
-/
51-
structure TokenInfo extends Fragment where
49+
structure SemanticTokenInfo extends Fragment where
5250
semanticType: Option String := none
5351
deriving Inhabited
5452

5553
/--
5654
The `TypeTokenInfo` describes the metadata of a source text token that conforms
57-
to a specific type within our type system.
58-
E.g.: A variable token `p` might conform to type `Nat`
55+
to a specific type within our type system. It may also describe just a docstring associated with the token.
56+
We combine both these informatio together, as the docstring is naturally attached to the type.
57+
E.g.: A variable token `p` might conform to type `Nat` and has the docstring for `Nat`
5958
-/
60-
structure TypeTokenInfo extends TokenInfo where
61-
type: String
59+
structure TypeTokenInfo extends Fragment where
60+
type: Option String
61+
docString: Option String
6262
deriving Inhabited
6363

6464
instance : Positional TypeTokenInfo where
6565
headPos := (λ x => x.toFragment.headPos)
6666
tailPos := (λ x => x.toFragment.tailPos)
67-
68-
/--
69-
The `DocStringTokenInfo` describes the metadata of an available docstring of a source text token.
70-
In this case a source text token may be a function or structure with a docstring.
71-
-/
72-
structure DocStringTokenInfo extends TokenInfo where
73-
docString: String
74-
deriving Inhabited
75-
76-
instance : Positional DocStringTokenInfo where
77-
headPos := (λ x => x.toFragment.headPos)
78-
tailPos := (λ x => x.toFragment.tailPos)
7967

8068
/--
8169
A `Token` describes the metadata of a specific range of source text.
@@ -86,25 +74,25 @@ instance : Positional DocStringTokenInfo where
8674
-/
8775
inductive Token where
8876
| type (info: TypeTokenInfo)
89-
| docString (info: DocStringTokenInfo)
77+
| semantic (info: SemanticTokenInfo)
9078
deriving Inhabited
9179

92-
instance : ToString Token where -- TODO: Improve this
80+
instance : ToString Token where
9381
toString : Token -> String
9482
| Token.type _ => "Type"
95-
| Token.docString _ => "DocString"
83+
| Token.semantic _ => "Semantic"
9684

9785
namespace Token
9886
def toFragment : Token -> Fragment
9987
| type info => info.toFragment
100-
| docString info => info.toFragment
88+
| semantic info => info.toFragment
10189

10290
def toTypeTokenInfo? : Token -> Option TypeTokenInfo
10391
| type info => info
10492
| _ => none
10593

106-
def toDocStringTokenInfo? : Token -> Option DocStringTokenInfo
107-
| docString info => info
94+
def toSemanticTokenInfo? : Token -> Option SemanticTokenInfo
95+
| semantic info => info
10896
| _ => none
10997
end Token
11098

@@ -239,25 +227,23 @@ namespace TraversalFragment
239227
let elabInfo := fragment.info
240228
return ← findDocString? env elabInfo.elaborator <||> findDocString? env elabInfo.stx.getKind
241229

242-
def genDocStringTokenInfo? (self : TraversalFragment) : AnalysisM (Option DocStringTokenInfo) := do
243-
match ← runMetaM (genDocString?) self with
244-
| some string => some { headPos := self.headPos, tailPos := self.tailPos, docString := string }
245-
| none => none
246-
247230
def genTypeTokenInfo? (self : TraversalFragment) : AnalysisM (Option TypeTokenInfo) := do
248-
match ← runMetaM (inferType?) self with
249-
| some string => some { headPos := self.headPos, tailPos := self.tailPos, type := string }
250-
| none => none
231+
let mut docString : Option String := none
232+
let mut type : Option String := none
233+
let config ← read
234+
if config.experimentalDocString then
235+
docString ← runMetaM (genDocString?) self
236+
if config.experimentalTypeInfo then
237+
type ← runMetaM (inferType?) self
238+
if type == none ∧ docString == none then
239+
return none
240+
else
241+
return some { headPos := self.headPos, tailPos := self.tailPos, type := type, docString := docString }
251242

252243
def genTokens (self : TraversalFragment) : AnalysisM (List Token) := do
253244
let mut tokens : List Token := []
254-
let config ← read
255-
if config.experimentalTypeInfo then
256-
if let some typeToken ← self.genTypeTokenInfo? then
257-
tokens := tokens.append [Token.type typeToken]
258-
if config.experimentalDocString then
259-
if let some docStringToken ← self.genDocStringTokenInfo? then
260-
tokens := tokens.append [Token.docString docStringToken]
245+
if let some typeToken ← self.genTypeTokenInfo? then
246+
tokens := tokens.append [Token.type typeToken]
261247
return tokens
262248

263249
/- Sentence Generation -/

LeanInk/Annotation/Alectryon.lean

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -104,25 +104,21 @@ instance : ToJson Fragment where
104104
Token Generation
105105
-/
106106

107-
def genTypeInfo (name : String) (info : Analysis.TypeTokenInfo) : TypeInfo := { name := name, type := info.type }
108-
109-
def genTypeInfo? (getContents : String.Pos -> String.Pos -> String) (tokens : List Analysis.TypeTokenInfo) : AnalysisM (Option TypeInfo) :=
110-
match Positional.smallest? tokens with
111-
| some token => do
107+
def genTypeInfo? (getContents : String.Pos -> String.Pos -> String) (token : Analysis.TypeTokenInfo) : AnalysisM (Option TypeInfo) := do
108+
match token.type with
109+
| some type => do
112110
let headPos := Positional.headPos token
113111
let tailPos := Positional.tailPos token
114-
return genTypeInfo (getContents headPos tailPos) token
112+
return some { name := (getContents headPos tailPos), type := type }
115113
| none => none
116114

117-
def genDocString? (tokens : List Analysis.DocStringTokenInfo) : AnalysisM (Option String) :=
118-
match Positional.smallest? tokens with
119-
| some token => token.docString
120-
| none => none
121-
122-
def genToken (token : Compound Analysis.Token) (contents : String) (getContents : String.Pos -> String.Pos -> String) : AnalysisM Token :=
123-
let typeInfo := genTypeInfo? getContents (token.getFragments.filterMap (λ x => x.toTypeTokenInfo?))
124-
let docString := genDocString? (token.getFragments.filterMap (λ x => x.toDocStringTokenInfo?))
125-
return { raw := contents, typeinfo := ← typeInfo, docstring := ← docString }
115+
def genToken (token : Compound Analysis.Token) (contents : String) (getContents : String.Pos -> String.Pos -> String) : AnalysisM Token := do
116+
let typeTokens := token.getFragments.filterMap (λ x => x.toTypeTokenInfo?)
117+
match (Positional.smallest? typeTokens) with
118+
| none => do
119+
return { raw := contents }
120+
| some token => do
121+
return { raw := contents, typeinfo := ← genTypeInfo? getContents token, link := none, docstring := token.docString }
126122

127123
def extractContents (offset : String.Pos) (contents : String) (head tail: String.Pos) : String :=
128124
if head >= tail then

0 commit comments

Comments
 (0)