From b77681d208bcdf77d9d758b98c44b6562db7e6c5 Mon Sep 17 00:00:00 2001 From: antew Date: Fri, 3 May 2019 23:08:33 -0400 Subject: [PATCH] Initial "working" version, replaces the whole file content at the moment. --- src/Analyser.elm | 51 ++++++++++++++++++++++++++++++++++++ src/Analyser/CodeBase.elm | 8 +++++- src/Analyser/FileContext.elm | 2 +- src/Analyser/Fixer.elm | 20 +++++++++++++- src/AnalyserPorts.elm | 8 +++++- ts/domain.ts | 6 +++++ 6 files changed, 91 insertions(+), 4 deletions(-) diff --git a/src/Analyser.elm b/src/Analyser.elm index cfbf8e7b..1090c763 100644 --- a/src/Analyser.elm +++ b/src/Analyser.elm @@ -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 @@ -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 @@ -45,6 +47,7 @@ type Msg | ReloadTick | Reset | OnFixMessage Int + | OnQuickFixMessage Int | FixerMsg Fixer.Msg @@ -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 ) @@ -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 diff --git a/src/Analyser/CodeBase.elm b/src/Analyser/CodeBase.elm index bd51d182..ec55e4b5 100644 --- a/src/Analyser/CodeBase.elm +++ b/src/Analyser/CodeBase.elm @@ -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 @@ -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 diff --git a/src/Analyser/FileContext.elm b/src/Analyser/FileContext.elm index 0e74db05..2cb75c2d 100644 --- a/src/Analyser/FileContext.elm +++ b/src/Analyser/FileContext.elm @@ -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) diff --git a/src/Analyser/Fixer.elm b/src/Analyser/Fixer.elm index 35ca35a7..e16062c0 100644 --- a/src/Analyser/Fixer.elm +++ b/src/Analyser/Fixer.elm @@ -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) @@ -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 diff --git a/src/AnalyserPorts.elm b/src/AnalyserPorts.elm index d4ba0093..a0c441a8 100644 --- a/src/AnalyserPorts.elm +++ b/src/AnalyserPorts.elm @@ -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) @@ -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 diff --git a/ts/domain.ts b/ts/domain.ts index 3c673cca..9c369e95 100644 --- a/ts/domain.ts +++ b/ts/domain.ts @@ -42,6 +42,10 @@ export interface AstStore { sha1: string; ast: JSON; } +export interface FixedFile { + path : string, + content: string +} export interface LogMessage { level: string; @@ -52,6 +56,7 @@ interface ElmApp { log: Subscription; sendReportValue: Subscription; sendState: Subscription; + sendFixedFile: Subscription; loadContext: Subscription; loadDependencyFiles: Subscription; loadFile: Subscription; @@ -65,6 +70,7 @@ interface ElmApp { fileWatch: MailBox; onReset: MailBox; onFixMessage: MailBox; + onFixQuick: MailBox; onLoadedContext: MailBox; onDependencyFiles: MailBox; fileContent: MailBox;