Skip to content

Commit

Permalink
Initial "working" version, replaces the whole file content at the mom…
Browse files Browse the repository at this point in the history
…ent.
  • Loading branch information
antew committed Jun 5, 2019
1 parent 3ad55f5 commit b77681d
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 4 deletions.
51 changes: 51 additions & 0 deletions src/Analyser.elm
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Analyser.CodeBase as CodeBase exposing (CodeBase)
import Analyser.Configuration as Configuration exposing (Configuration)
import Analyser.ContextLoader as ContextLoader exposing (Context)
import Analyser.DependencyLoadingStage as DependencyLoadingStage
import Analyser.FileContext as FileContext
import Analyser.FileWatch as FileWatch exposing (FileChange(..))
import Analyser.Files.Types exposing (LoadedSourceFile)
import Analyser.Fixer as Fixer
Expand All @@ -18,6 +19,7 @@ import Elm.Version
import Inspection
import Json.Decode
import Json.Encode exposing (Value)
import Maybe.Extra as Maybe
import Platform exposing (worker)
import Registry exposing (Registry)
import Time
Expand Down Expand Up @@ -45,6 +47,7 @@ type Msg
| ReloadTick
| Reset
| OnFixMessage Int
| OnQuickFixMessage Int
| FixerMsg Fixer.Msg


Expand Down Expand Up @@ -141,6 +144,53 @@ update msg model =
)
|> handleNextStep

OnQuickFixMessage messageId ->
let
processingContext =
CodeBase.processContext model.codeBase

maybeMessage =
State.getMessage messageId model.state

maybeFixedFile =
case maybeMessage of
Just message ->
CodeBase.getFile message.file.path model.codeBase
|> Maybe.map
(\loadedSourceFile ->
( .content <| Tuple.first loadedSourceFile
, FileContext.buildForFile processingContext loadedSourceFile
|> Maybe.map .ast
)
)
|> Maybe.map
(\sources ->
case sources of
( Just fileText, Just ast ) ->
Just <| Fixer.fixFast message ( fileText, ast )

_ ->
Nothing
)
|> Maybe.join

Nothing ->
Nothing
in
case ( maybeMessage, maybeFixedFile ) of
( Just message, Just (Ok fixedFile) ) ->
( model
, AnalyserPorts.sendFixedFile
{ path = message.file.path
, content = fixedFile
}
)

_ ->
( model
, Logger.info ("Unable to apply fix for message id: " ++ String.fromInt messageId)
)

Reset ->
reset ( model, Cmd.none )

Expand Down Expand Up @@ -419,6 +469,7 @@ subscriptions model =
Sub.none
, FileWatch.watcher Change
, AnalyserPorts.onFixMessage OnFixMessage
, AnalyserPorts.onFixQuick OnQuickFixMessage
, case model.stage of
ContextLoadingStage ->
ContextLoader.onLoadedContext OnContext
Expand Down
8 changes: 7 additions & 1 deletion src/Analyser/CodeBase.elm
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
module Analyser.CodeBase exposing (CodeBase, addSourceFiles, dependencies, init, mergeLoadedSourceFiles, processContext, setDependencies, sourceFiles)
module Analyser.CodeBase exposing (CodeBase, addSourceFiles, dependencies, getFile, init, mergeLoadedSourceFiles, processContext, setDependencies, sourceFiles)

import Analyser.Files.Types exposing (LoadedSourceFile)
import Dict exposing (Dict)
import Elm.Dependency exposing (Dependency)
import Elm.Processing as Processing exposing (ProcessContext)
import Elm.Syntax.File exposing (File)


type CodeBase
Expand Down Expand Up @@ -62,3 +63,8 @@ addSourceFiles sources (CodeBase codeBase) =
mergeLoadedSourceFiles : List LoadedSourceFile -> Dict String LoadedSourceFile -> Dict String LoadedSourceFile
mergeLoadedSourceFiles newItems dict =
List.foldl (\sourceFile -> Dict.insert (Tuple.first sourceFile).path sourceFile) dict newItems


getFile : String -> CodeBase -> Maybe LoadedSourceFile
getFile path (CodeBase codeBase) =
Dict.get path codeBase.sources
2 changes: 1 addition & 1 deletion src/Analyser/FileContext.elm
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Analyser.FileContext exposing (FileContext, build, moduleName)
module Analyser.FileContext exposing (FileContext, build, buildForFile, moduleName)

import Analyser.CodeBase as CodeBase exposing (CodeBase)
import Analyser.FileRef exposing (FileRef)
Expand Down
20 changes: 19 additions & 1 deletion src/Analyser/Fixer.elm
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
port module Analyser.Fixer exposing (Model, Msg, init, initWithMessage, isDone, message, subscriptions, succeeded, update)
port module Analyser.Fixer exposing (Model, Msg, fixFast, init, initWithMessage, isDone, message, subscriptions, succeeded, update)

import Analyser.CodeBase as CodeBase exposing (CodeBase)
import Analyser.FileRef exposing (FileRef)
Expand Down Expand Up @@ -71,6 +71,24 @@ initWithMessage mess state =
)


fixFast : Message -> ( String, File ) -> Result String String
fixFast mess pathAndFile =
Analyser.Fixers.getFixer mess
|> Maybe.map
(\fixer ->
case fixer.fix pathAndFile mess.data of
Error e ->
Err e

Patched p ->
Ok p

IncompatibleData ->
Err ("Invalid message data for fixer, message id: " ++ String.fromInt mess.id)
)
|> Maybe.withDefault (Err "Unable to find fixer")


isDone : Model -> Bool
isDone (Model m) =
m.done
Expand Down
8 changes: 7 additions & 1 deletion src/AnalyserPorts.elm
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
port module AnalyserPorts exposing (onFixMessage, onReset, sendReport, sendStateValue)
port module AnalyserPorts exposing (onFixMessage, onFixQuick, onReset, sendFixedFile, sendReport, sendStateValue)

import Analyser.Report as Report exposing (Report)
import Analyser.State exposing (State, encodeState)
Expand All @@ -11,12 +11,18 @@ port sendReportValue : Value -> Cmd msg
port sendState : Value -> Cmd msg


port sendFixedFile : { path : String, content : String } -> Cmd msg


port onReset : (Bool -> msg) -> Sub msg


port onFixMessage : (Int -> msg) -> Sub msg


port onFixQuick : (Int -> msg) -> Sub msg


sendReport : Report -> Cmd msg
sendReport =
sendReportValue << Report.encode
Expand Down
6 changes: 6 additions & 0 deletions ts/domain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ export interface AstStore {
sha1: string;
ast: JSON;
}
export interface FixedFile {
path : string,
content: string
}

export interface LogMessage {
level: string;
Expand All @@ -52,6 +56,7 @@ interface ElmApp {
log: Subscription<LogMessage>;
sendReportValue: Subscription<Report>;
sendState: Subscription<State>;
sendFixedFile: Subscription<FixedFile>;
loadContext: Subscription<void>;
loadDependencyFiles: Subscription<DependencyPointer>;
loadFile: Subscription<string>;
Expand All @@ -65,6 +70,7 @@ interface ElmApp {
fileWatch: MailBox<FileChange>;
onReset: MailBox<boolean>;
onFixMessage: MailBox<number>;
onFixQuick: MailBox<number>;
onLoadedContext: MailBox<Context>;
onDependencyFiles: MailBox<DependencyFiles>;
fileContent: MailBox<FileContent>;
Expand Down

0 comments on commit b77681d

Please sign in to comment.