Skip to content

Commit

Permalink
Merge pull request #209 from antew/feature/lsp-fixer
Browse files Browse the repository at this point in the history
LSP helpers - Allow sending fixed file content back without writing to disk
  • Loading branch information
stil4m authored Jul 8, 2019
2 parents 3ad55f5 + f0715e5 commit d16c93d
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 6 deletions.
44 changes: 44 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 exposing (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,38 @@ update msg model =
)
|> handleNextStep

OnQuickFixMessage messageId ->
let
applyFix message =
getFileContext message.file.path model.codeBase
|> Maybe.map (Fixer.getFixedFile message)
|> Maybe.withDefault
(Err ("Unable to fix messageId: " ++ String.fromInt messageId))
in
case State.getMessage messageId model.state of
Just message ->
case applyFix message of
Ok fixedFile ->
( model
, AnalyserPorts.sendFixedFile
{ path = message.file.path
, content = fixedFile
}
)

Err err ->
( model
, Logger.error err
)

Nothing ->
( model
, Logger.info
("Unable to apply fix, unable to find messageId: "
++ String.fromInt messageId
)
)

Reset ->
reset ( model, Cmd.none )

Expand Down Expand Up @@ -408,6 +443,14 @@ onSourceLoadingStageMsg x stage model =
)


getFileContext : String -> CodeBase -> Maybe FileContext
getFileContext path codeBase =
CodeBase.getFile path codeBase
|> Maybe.map
(FileContext.buildForFile (CodeBase.processContext codeBase))
|> Maybe.join


subscriptions : Model -> Sub Msg
subscriptions model =
Sub.batch
Expand All @@ -419,6 +462,7 @@ subscriptions model =
Sub.none
, FileWatch.watcher Change
, AnalyserPorts.onFixMessage OnFixMessage
, AnalyserPorts.onFixQuick OnQuickFixMessage
, case model.stage of
ContextLoadingStage ->
ContextLoader.onLoadedContext OnContext
Expand Down
7 changes: 6 additions & 1 deletion src/Analyser/CodeBase.elm
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
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)
Expand Down Expand Up @@ -62,3 +62,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
21 changes: 20 additions & 1 deletion src/Analyser/Fixer.elm
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
port module Analyser.Fixer exposing (Model, Msg, init, initWithMessage, isDone, message, subscriptions, succeeded, update)
port module Analyser.Fixer exposing (Model, Msg, getFixedFile, init, initWithMessage, isDone, message, subscriptions, succeeded, update)

import Analyser.CodeBase as CodeBase exposing (CodeBase)
import Analyser.FileContext exposing (FileContext)
import Analyser.FileRef exposing (FileRef)
import Analyser.Fixers
import Analyser.Fixes.Base exposing (Fixer, Patch(..))
Expand Down Expand Up @@ -71,6 +72,24 @@ initWithMessage mess state =
)


getFixedFile : Message -> FileContext -> Result String String
getFixedFile mess fileContext =
Analyser.Fixers.getFixer mess
|> Maybe.map
(\fixer ->
case fixer.fix ( fileContext.content, fileContext.ast ) 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
11 changes: 9 additions & 2 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 Expand Up @@ -99,7 +105,8 @@ interface EditorElmApp {
}

interface Subscription<T> {
subscribe: ((cb: ((d: T) => void)) => void);
subscribe: (cb: (d: T) => void) => void;
unsubscribe: (cb: (d: T) => void) => void;
}

interface FileContentSha {
Expand All @@ -116,7 +123,7 @@ interface Context {
configuration: string;
}
interface MailBox<T> {
send: ((d: T) => void);
send: (d: T) => void;
}

interface Reporter {
Expand Down

0 comments on commit d16c93d

Please sign in to comment.