Skip to content

Commit

Permalink
Merge branch 'main' of github.com:jxxcarlson/elm-review-codeinstaller
Browse files Browse the repository at this point in the history
  • Loading branch information
jxxcarlson committed Jul 16, 2024
2 parents fd092fb + b812f46 commit 9e8ae94
Show file tree
Hide file tree
Showing 3 changed files with 296 additions and 36 deletions.
78 changes: 49 additions & 29 deletions src/Install/Initializer.elm
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import Elm.Syntax.ModuleName exposing (ModuleName)
import Elm.Syntax.Node as Node exposing (Node(..))
import Elm.Syntax.Range exposing (Range)
import Install.Library
import List.Extra
import Review.Fix as Fix exposing (Fix)
import Review.Rule as Rule exposing (Error, Rule)
import Set exposing (Set)
Expand Down Expand Up @@ -93,10 +94,6 @@ declarationVisitor moduleName functionName data (Node _ declaration) context =

isInCorrectModule =
Install.Library.isInCorrectModule moduleName context

namespace : String
namespace =
String.join "." context.moduleName ++ "." ++ name
in
if name == functionName && isInCorrectModule then
visitFunction data Set.empty function context
Expand All @@ -116,30 +113,7 @@ visitFunction data ignored function context =
Node.value function.declaration

( fieldNames, lastRange ) =
case declaration.expression |> Node.value of
TupledExpression expressions ->
let
lastRange_ =
case expressions |> List.map Node.value |> List.head of
Just recordExpr ->
Install.Library.lastRange recordExpr

Nothing ->
Elm.Syntax.Range.empty

fieldNames_ : List String
fieldNames_ =
case expressions |> List.map Node.value |> List.head of
Just recordExpr ->
Install.Library.fieldNames recordExpr

Nothing ->
[]
in
( fieldNames_, lastRange_ )

_ ->
( [], Elm.Syntax.Range.empty )
getFieldNamesAndLastRange (Node.value declaration.expression)

existingFields =
Set.fromList fieldNames
Expand Down Expand Up @@ -180,10 +154,56 @@ addMissingCases : { row : Int, column : Int } -> List { field : String, value :
addMissingCases insertionPoint data =
let
insertion =
"\n , " ++ (List.map (\{ field, value } -> field ++ " = " ++ value) data |> String.join "\n , ")
", " ++ (List.map (\{ field, value } -> field ++ " = " ++ value) data |> String.join ", ")
in
Fix.insertAt
{ row = insertionPoint.row
, column = insertionPoint.column + 4
}
insertion


getFieldNamesAndLastRange : Expression -> ( List String, Range )
getFieldNamesAndLastRange expr =
case expr of
TupledExpression expressions ->
let
lastRange_ =
case expressions |> List.head |> Maybe.map Node.value of
Just expression ->
Install.Library.lastRange expression

Nothing ->
Elm.Syntax.Range.empty

fieldNames_ : List String
fieldNames_ =
case expressions |> List.map Node.value |> List.head of
Just recordExpr ->
Install.Library.fieldNames recordExpr

Nothing ->
[]
in
( fieldNames_, lastRange_ )

LetExpression { expression } ->
getFieldNamesAndLastRange (Node.value expression)

Application children ->
children
|> List.Extra.find
(\child ->
case Node.value child of
TupledExpression _ ->
True

_ ->
False
)
|> Maybe.map Node.value
|> Maybe.withDefault (TupledExpression [])
|> getFieldNamesAndLastRange

_ ->
( [], Elm.Syntax.Range.empty )
30 changes: 29 additions & 1 deletion src/Install/Library.elm
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import Elm.Syntax.ModuleName exposing (ModuleName)
import Elm.Syntax.Node as Node exposing (Node(..))
import Elm.Syntax.Pattern exposing (Pattern(..))
import Elm.Syntax.Range as Range exposing (Range)
import List.Extra
import Review.ModuleNameLookupTable as ModuleNameLookupTable exposing (ModuleNameLookupTable)
import Review.Rule as Rule
import Set exposing (Set)
Expand All @@ -28,6 +29,9 @@ fieldNames expr =
RecordExpr fields ->
List.map (\(Node _ ( name, _ )) -> Node.value name) fields

Application children ->
List.concatMap (Node.value >> fieldNames) children

_ ->
[]

Expand All @@ -36,9 +40,33 @@ lastRange : Expression -> Range
lastRange expr =
case expr of
RecordExpr fields ->
List.map (\(Node rg _) -> rg) fields
fields
|> List.reverse
|> List.head
|> Maybe.map
(\setter ->
Node.value setter
|> Tuple.second
|> Node.range
)
|> Maybe.withDefault Range.empty

Application children ->
List.Extra.findMap
(\child ->
let
expression =
Node.value child
in
case expression of
RecordExpr _ ->
Just expression

_ ->
Nothing
)
children
|> Maybe.map lastRange
|> Maybe.withDefault Range.empty

_ ->
Expand Down
Loading

0 comments on commit 9e8ae94

Please sign in to comment.