diff --git a/src/Install/ClauseInCase.elm b/src/Install/ClauseInCase.elm index 38a9aad..11c467b 100644 --- a/src/Install/ClauseInCase.elm +++ b/src/Install/ClauseInCase.elm @@ -1,4 +1,7 @@ -module Install.ClauseInCase exposing (init, makeRule, withInsertAfter, withCustomErrorMessage, Config, CustomError) +module Install.ClauseInCase exposing + ( init, makeRule, withInsertAfter, withCustomErrorMessage, Config, CustomError + , withInsertAtBeginning + ) {-| Add a clause to a case expression in a specified function in a specified module. For example, if you put the code below in your @@ -240,8 +243,12 @@ rangeToInsertClause insertAt cases expression = ( Node.range lastClauseExpression, 2, 0 ) AtBeginning -> - -- TODO: this code is not correct - ( Node.range expression, 1, (Node.range expression).start.column ) + let + -- The clause must always be indented 3 columns after the start of the case expression + firstClauseOffset = + (Node.range expression).start.column + 3 + in + ( Node.range expression, 1, firstClauseOffset ) AtEnd -> let diff --git a/tests/Install/ClauseInCaseTest2.elm b/tests/Install/ClauseInCaseTest2.elm index 5fe5968..09111f5 100644 --- a/tests/Install/ClauseInCaseTest2.elm +++ b/tests/Install/ClauseInCaseTest2.elm @@ -10,8 +10,8 @@ all = describe "Install.ClauseInCase" [ Run.testFix test1a , Run.testFix test1b + , Run.testFix test1c - -- Run.testFix test1c -- Run.testFix test2 ] @@ -43,7 +43,7 @@ test1b = test1c = { description = "Test 1c, withInsertAtBeginning: should report an error and fix it" , src = src1 - , rule = rule1b + , rule = rule1c , under = under1c , fixed = fixed1c , message = "Add handler for ResetCounter" @@ -61,12 +61,10 @@ rule1b = |> Install.ClauseInCase.makeRule - --- ---rule1c = --- Install.ClauseInCase.init "Backend" "updateFromFrontend" "ResetCounter" "( { model | counter = 0 }, broadcast (CounterNewValue 0 clientId) )" --- |> Install.ClauseInCase.withInsertAtBeginning --- |> Install.ClauseInCase.makeRule +rule1c = + Install.ClauseInCase.init "Backend" "updateFromFrontend" "ResetCounter" "( { model | counter = 0 }, broadcast (CounterNewValue 0 clientId) )" + |> Install.ClauseInCase.withInsertAtBeginning + |> Install.ClauseInCase.makeRule src1 = @@ -112,6 +110,7 @@ fixed1c = updateFromFrontend : SessionId -> ClientId -> ToBackend -> Model -> ( Model, Cmd BackendMsg ) updateFromFrontend sessionId clientId msg model = case msg of + ResetCounter -> ( { model | counter = 0 }, broadcast (CounterNewValue 0 clientId) ) CounterIncremented -> @@ -121,6 +120,7 @@ updateFromFrontend sessionId clientId msg model = in ( { model | counter = newCounter }, broadcast (CounterNewValue newCounter clientId) ) + """ @@ -136,7 +136,12 @@ under1 = under1c = """case msg of - """ + CounterIncremented -> + let + newCounter = + model.counter + 1 + in + ( { model | counter = newCounter }, broadcast (CounterNewValue newCounter clientId) )"""