Skip to content
This repository has been archived by the owner on Jul 19, 2022. It is now read-only.

Commit

Permalink
Remove Code.Finder's dependecy on Api
Browse files Browse the repository at this point in the history
  • Loading branch information
hojberg committed Apr 6, 2022
1 parent 24f897b commit 3e5dced
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 24 deletions.
8 changes: 8 additions & 0 deletions src/Api.elm
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module Api exposing
( ApiBasePath(..)
, ApiRequest
, codebaseApiEndpointToEndpointUrl
, codebaseHash
, find
, getDefinition
Expand All @@ -13,12 +14,14 @@ module Api exposing
, toUrl
)

import Code.CodebaseApi exposing (CodebaseEndpoint)
import Code.FullyQualifiedName as FQN exposing (FQN)
import Code.Hash as Hash exposing (Hash)
import Code.Perspective as Perspective exposing (Perspective(..))
import Code.Syntax as Syntax
import Http
import Json.Decode as Decode
import Lib.Api
import Regex
import Task exposing (Task)
import Url.Builder exposing (QueryParameter, absolute, int, string)
Expand Down Expand Up @@ -110,6 +113,11 @@ find perspective withinFqn limit (Syntax.Width sourceWidth) query =
)


codebaseApiEndpointToEndpointUrl : CodebaseEndpoint -> Lib.Api.EndpointUrl
codebaseApiEndpointToEndpointUrl _ =
Lib.Api.EndpointUrl [] []



-- REQUEST --------------------------------------------------------------------

Expand Down
54 changes: 35 additions & 19 deletions src/Code/Finder.elm
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
module Code.Finder exposing (Model, Msg, OutMsg(..), init, update, view)

import Api exposing (ApiRequest)
import Browser.Dom as Dom
import Code.CodebaseApi as CodebaseApi
import Code.Config exposing (Config)
import Code.Definition.AbilityConstructor exposing (AbilityConstructor(..))
import Code.Definition.Category as Category
import Code.Definition.DataConstructor exposing (DataConstructor(..))
Expand All @@ -15,7 +16,6 @@ import Code.FullyQualifiedName as FQN exposing (FQN)
import Code.HashQualified exposing (HashQualified(..))
import Code.Perspective as Perspective exposing (Perspective)
import Code.Syntax as Syntax
import Env exposing (Env)
import Html
exposing
( Html
Expand Down Expand Up @@ -49,6 +49,7 @@ import Html.Attributes
)
import Html.Events exposing (onClick, onInput)
import Http
import Lib.Api as Api exposing (ApiRequest)
import Lib.SearchResults as SearchResults exposing (SearchResults(..))
import Lib.Util as Util
import List.Nonempty as NEL
Expand Down Expand Up @@ -84,11 +85,11 @@ type alias Model =
}


init : Env -> SearchOptions -> ( Model, Cmd Msg )
init env options =
init : Config -> SearchOptions -> ( Model, Cmd Msg )
init config options =
( { input = ""
, search = NotAsked
, keyboardShortcut = KeyboardShortcut.init env.operatingSystem
, keyboardShortcut = KeyboardShortcut.init config.operatingSystem
, options = options
}
, focusSearchInput
Expand Down Expand Up @@ -118,8 +119,8 @@ type OutMsg
| OpenDefinition Reference


update : Env -> Msg -> Model -> ( Model, Cmd Msg, OutMsg )
update env msg model =
update : Config -> Msg -> Model -> ( Model, Cmd Msg, OutMsg )
update config msg model =
let
debounceDelay =
300
Expand Down Expand Up @@ -168,9 +169,9 @@ update env msg model =
if query == model.input then
let
( search, fetch ) =
performSearch env.perspective model.options model.search query
performSearch config model.options model.search query
in
( { model | search = search }, Api.perform env.apiBasePath fetch, Remain )
( { model | search = search }, Api.perform config.apiBasePath fetch, Remain )

else
( model, Cmd.none, Remain )
Expand All @@ -197,16 +198,16 @@ update env msg model =
RemoveWithinOption ->
let
options =
SearchOptions.removeWithin env.perspective model.options
SearchOptions.removeWithin config.perspective model.options

-- Don't perform search when the query is empty.
( search, cmd ) =
if not (String.isEmpty model.input) then
let
( search_, fetch ) =
performSearch env.perspective options model.search model.input
performSearch config options model.search model.input
in
( search_, Api.perform env.apiBasePath fetch )
( search_, Api.perform config.apiBasePath fetch )

else
( model.search, Cmd.none )
Expand Down Expand Up @@ -331,12 +332,12 @@ finderSearchToMaybe fs =


performSearch :
Perspective
Config
-> SearchOptions
-> FinderSearch
-> String
-> ( FinderSearch, ApiRequest (List FinderMatch) Msg )
performSearch perspective options search query =
performSearch { toApiEndpointUrl, perspective } options search query =
let
search_ =
case search of
Expand All @@ -353,29 +354,44 @@ performSearch perspective options search query =
case options of
SearchOptions AllNamespaces ->
fetchMatches
toApiEndpointUrl
(Perspective.toCodebasePerspective perspective)
Nothing
query

SearchOptions (WithinNamespacePerspective _) ->
fetchMatches perspective Nothing query
fetchMatches toApiEndpointUrl
perspective
Nothing
query

SearchOptions (WithinNamespace fqn) ->
fetchMatches perspective (Just fqn) query
fetchMatches
toApiEndpointUrl
perspective
(Just fqn)
query
in
( search_, fetch )


fetchMatches : Perspective -> Maybe FQN -> String -> ApiRequest (List FinderMatch) Msg
fetchMatches perspective withinFqn query =
fetchMatches : CodebaseApi.ToApiEndpointUrl -> Perspective -> Maybe FQN -> String -> ApiRequest (List FinderMatch) Msg
fetchMatches toApiEndpointUrl perspective withinFqn query =
let
limit =
9

sourceWidth =
Syntax.Width 100
in
Api.find perspective withinFqn limit sourceWidth query
CodebaseApi.Find
{ perspective = perspective
, withinFqn = withinFqn
, limit = limit
, sourceWidth = sourceWidth
, query = query
}
|> toApiEndpointUrl
|> Api.toRequest FinderMatch.decodeMatches (FetchMatchesFinished query)


Expand Down
11 changes: 9 additions & 2 deletions src/UnisonLocal/App.elm
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ type Msg

update : Msg -> Model -> ( Model, Cmd Msg )
update msg ({ env } as model) =
let
codebaseConfig =
Env.toCodeConfig Api.codebaseApiEndpointToEndpointUrl env
in
case msg of
LinkClicked urlRequest ->
case urlRequest of
Expand Down Expand Up @@ -270,7 +274,7 @@ update msg ({ env } as model) =
FinderModal fModel ->
let
( fm, fc, out ) =
Finder.update env (Api.performCodebaseApiRequest env.apiBasePath) fMsg fModel
Finder.update codebaseConfig fMsg fModel
in
case out of
Finder.Remain ->
Expand Down Expand Up @@ -411,11 +415,14 @@ showFinder :
-> ( { m | env : Env, modal : Modal }, Cmd Msg )
showFinder model withinNamespace =
let
codebaseConfig =
Env.toCodeConfig Api.codebaseApiEndpointToEndpointUrl model.env

options =
SearchOptions.init model.env.perspective withinNamespace

( fm, fcmd ) =
Finder.init model.env options
Finder.init codebaseConfig options
in
( { model | modal = FinderModal fm }, Cmd.map FinderMsg fcmd )

Expand Down
14 changes: 11 additions & 3 deletions src/UnisonShare/AppModal.elm
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
module UnisonShare.AppModal exposing (..)

import Api
import Code.Definition.Reference exposing (Reference)
import Code.Finder as Finder
import Code.Finder.SearchOptions as SearchOptions
import Code.FullyQualifiedName as FQN exposing (FQN)
import Env exposing (Env, OperatingSystem(..))
import Env exposing (Env)
import Html exposing (Html, a, div, h3, p, section, span, strong, text)
import Html.Attributes exposing (class, href, rel, target)
import Lib.OperatingSystem exposing (OperatingSystem(..))
import UI
import UI.Button as Button
import UI.CopyField as CopyField
Expand Down Expand Up @@ -56,8 +58,11 @@ update env msg model =
case ( model, msg ) of
( Visible (FinderModal fModel), FinderMsg fMsg ) ->
let
codebaseConfig =
Env.toCodeConfig Api.codebaseApiEndpointToEndpointUrl env

( fm, fc, out ) =
Finder.update env fMsg fModel
Finder.update codebaseConfig fMsg fModel
in
case out of
Finder.Remain ->
Expand Down Expand Up @@ -91,8 +96,11 @@ showFinder env withinNamespace =
options =
SearchOptions.init env.perspective withinNamespace

codebaseConfig =
Env.toCodeConfig Api.codebaseApiEndpointToEndpointUrl env

( fm, fcmd ) =
Finder.init env options
Finder.init codebaseConfig options
in
( Visible (FinderModal fm), Cmd.map FinderMsg fcmd )

Expand Down

0 comments on commit 3e5dced

Please sign in to comment.