Skip to content

Commit 5f3c33d

Browse files
Loogle all cases
1 parent 99f6367 commit 5f3c33d

File tree

1 file changed

+67
-12
lines changed

1 file changed

+67
-12
lines changed

LeanSearchClient/LoogleSyntax.lean

+67-12
Original file line numberDiff line numberDiff line change
@@ -126,13 +126,23 @@ If you pass more than one such search filter, separated by commas Loogle will re
126126
🔍 Real.sin, \"two\", tsum, _ * _, _ ^ _, |- _ < _ → _
127127
woould find all lemmas which mention the constants Real.sin and tsum, have \"two\" as a substring of the lemma name, include a product and a power somewhere in the type, and have a hypothesis of the form _ < _ (if there were any such lemmas). Metavariables (?a) are assigned independently in each filter."
128128

129+
/-- The turnstyle uesd bin `#find`, unicode or ascii allowed -/
130+
syntax turnstyle := patternIgnore("⊢ " <|> "|- ")
131+
/-- a single `#find` filter. The `term` can also be an ident or a strlit,
132+
these are distinguished in `parseFindFilters` -/
133+
syntax loogle_filter := (turnstyle term) <|> term
134+
135+
/-- The argument to `#find`, a list of filters -/
136+
syntax loogle_filters := loogle_filter,*
137+
129138
open Command
130-
syntax (name := loogle_cmd) "#loogle" str "go" : command
139+
syntax (name := loogle_cmd) "#loogle" loogle_filters "go" : command
131140
@[command_elab loogle_cmd] def loogleCmdImpl : CommandElab := fun stx =>
132141
Command.liftTermElabM do
133142
match stx with
134-
| `(command| #loogle $s go) =>
135-
let s := s.getString
143+
| `(command| #loogle $args:loogle_filters go) =>
144+
let s := (← PrettyPrinter.ppCategory ``loogle_filters args).pretty
145+
logInfo s
136146
let result ← getLoogleQueryJson s
137147
match result with
138148
| LoogleResult.success xs =>
@@ -149,28 +159,27 @@ syntax (name := loogle_cmd) "#loogle" str "go" : command
149159
| some suggestions =>
150160
let suggestions : List TryThis.Suggestion :=
151161
suggestions.map fun s =>
152-
let s := s.replace "\"" "\\\""
153162
{suggestion := .string s!"#loogle \"{s}\" go"}
154163
unless suggestions.isEmpty do
155164
TryThis.addSuggestions stx suggestions.toArray (header := s!"Did you maybe mean")
156165
| none => pure ()
157166
| _ => throwUnsupportedSyntax
158167

159-
-- #loogle "List ?a → ?b" go
168+
-- #loogle List ?a → ?b go
160169

161-
-- #loogle "nonsense" go
170+
-- #loogle nonsense go
162171

163-
-- #loogle "?a → ?b" go
172+
-- #loogle ?a → ?b go
164173

165-
-- #loogle "sin" go
174+
-- #loogle sin go
166175

167176

168-
syntax (name := loogle_term) "#loogle" str "go" : term
177+
syntax (name := loogle_term) "#loogle" loogle_filters "go" : term
169178
@[term_elab loogle_term] def loogleTermImpl : TermElab :=
170179
fun stx expectedType? => do
171180
match stx with
172-
| `(#loogle $s go) =>
173-
let s := s.getString
181+
| `(#loogle $args go) =>
182+
let s := (← PrettyPrinter.ppCategory ``loogle_filters args).pretty
174183
let result ← getLoogleQueryJson s
175184
match result with
176185
| LoogleResult.success xs =>
@@ -196,7 +205,53 @@ syntax (name := loogle_term) "#loogle" str "go" : term
196205
defaultTerm expectedType?
197206
| _ => throwUnsupportedSyntax
198207

208+
syntax (name := loogle_tactic) "#loogle" loogle_filters "go" : tactic
209+
@[tactic loogle_tactic] def loogleTacticImpl : Tactic :=
210+
fun stx => do
211+
match stx with
212+
| `(tactic|#loogle $args go) =>
213+
let s := (← PrettyPrinter.ppCategory ``loogle_filters args).pretty
214+
let result ← getLoogleQueryJson s
215+
match result with
216+
| LoogleResult.success xs => do
217+
let suggestionGroups := xs.map fun sr =>
218+
(sr.name, sr.toTacticSuggestions)
219+
for (name, sg) in suggestionGroups do
220+
let sg ← sg.filterM fun s =>
221+
let sugTxt := s.suggestion
222+
match sugTxt with
223+
| .string s => do
224+
let stx? := runParserCategory (← getEnv) `tactic s
225+
match stx? with
226+
| Except.ok stx =>
227+
let n? ← checkTactic (← getMainTarget) stx
228+
return n?.isSome
229+
| Except.error _ =>
230+
pure false
231+
| _ => pure false
232+
unless sg.isEmpty do
233+
TryThis.addSuggestions stx sg (header := s!"From: {name}")
234+
235+
| LoogleResult.failure error suggestions? =>
236+
logWarning s!"Loogle search failed with error: {error}"
237+
logInfo loogleUsage
238+
match suggestions? with
239+
| some suggestions =>
240+
let suggestions : List TryThis.Suggestion :=
241+
suggestions.map fun s =>
242+
{suggestion := .string s!"#loogle \"{s}\" go"}
243+
unless suggestions.isEmpty do
244+
TryThis.addSuggestions stx suggestions.toArray (header := s!"Did you maybe mean")
245+
| none => pure ()
246+
| _ => throwUnsupportedSyntax
247+
199248

200-
-- example := #loogle "List ?a → ?b" go
249+
example : 35 := by
250+
-- #loogle Nat.succ_le_succ go
251+
decide
252+
253+
-- example := #loogle List ?a → ?b go
201254

202255
end LeanSearchClient
256+
257+
-- #loogle "sin", Real → Real, |- Real go

0 commit comments

Comments
 (0)