Skip to content

Commit

Permalink
feat: add ability to click on types to see hexdocs
Browse files Browse the repository at this point in the history
Signed-off-by: Guillaume Hivert <[email protected]>
  • Loading branch information
ghivert committed May 13, 2024
1 parent c27ef89 commit 989b5dd
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 13 deletions.
18 changes: 7 additions & 11 deletions apps/frontend/src/data/search_result.gleam
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import data/kind.{type Kind}
import data/metadata.{type Metadata}
import data/signature.{type Signature}
import frontend/view/helpers
import gleam/dynamic
import gleam/function
import gleam/string

pub type SearchResult {
SearchResult(
Expand Down Expand Up @@ -52,13 +51,10 @@ pub fn decode_search_results(dyn) {
}

pub fn hexdocs_link(search_result: SearchResult) {
let join = function.flip(string.join)
let base =
join("/", [
"https://hexdocs.pm",
search_result.package_name,
search_result.version,
search_result.module_name,
])
base <> ".html#" <> search_result.name
helpers.hexdocs_link(
package: search_result.package_name,
version: search_result.version,
module: search_result.module_name,
name: search_result.name,
)
}
22 changes: 20 additions & 2 deletions apps/frontend/src/frontend/view/body/signature.gleam
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import data/msg
import data/search_result
import data/signature.{type Parameter, type Type, Parameter}
import frontend/view/body/styles as s
import frontend/view/helpers
import frontend/view/types as t
import gleam/bool
import gleam/int
import gleam/list
import gleam/option.{None, Some}
import gleam/string
import lustre/attribute as a
import lustre/element as el
import lustre/element/html as h

Expand Down Expand Up @@ -109,13 +111,29 @@ fn view_type(type_: Type, indent: Int) -> List(el.Element(msg.Msg)) {
}),
]
}
signature.Named(width, name, _package, _module, parameters, _ref) -> {
signature.Named(width, name, package, module, parameters, version) -> {
let inline = width + indent <= 80
let is_params = !list.is_empty(parameters)
list.concat([
[
helpers.idt(indent),
t.type_(name),
case version {
None -> t.type_(name)
Some(version) ->
s.named_type_button(
[
a.target("_blank"),
a.rel("noreferrer"),
a.href(helpers.hexdocs_link(
package: package,
version: version,
module: module,
name: name,
)),
],
[t.type_(name)],
)
},
case is_params {
True -> h.text("(")
False -> el.none()
Expand Down
8 changes: 8 additions & 0 deletions apps/frontend/src/frontend/view/body/styles.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -411,3 +411,11 @@ pub fn items_wrapper(attributes, children) {
|> list.prepend(attributes, _)
|> element.element("div", _, children)
}

pub fn named_type_button(attributes, children) {
s.class([s.text_decoration("none")])
|> s.memo()
|> s.to_lustre()
|> list.prepend(attributes, _)
|> element.element("a", _, children)
}
12 changes: 12 additions & 0 deletions apps/frontend/src/frontend/view/helpers.gleam
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import gleam/function
import gleam/string
import lustre/element/html as h

Expand All @@ -8,3 +9,14 @@ pub fn newline() {
pub fn idt(indent: Int) {
h.text(string.repeat(" ", indent))
}

pub fn hexdocs_link(
package package: String,
version version: String,
module module: String,
name name: String,
) {
let join = function.flip(string.join)
let base = join("/", ["https://hexdocs.pm", package, version, module])
base <> ".html#" <> name
}

0 comments on commit 989b5dd

Please sign in to comment.