Skip to content

Commit

Permalink
refactor: Reduce the number of options for formatting and show when c…
Browse files Browse the repository at this point in the history
…reating a hidden conf file
  • Loading branch information
tgodzik committed Oct 26, 2023
1 parent 7aaea84 commit 29c5efe
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -205,10 +205,6 @@ final class FormattingProvider(
tables.dismissedNotifications.ChangeScalafmtVersion
.dismiss(24, TimeUnit.HOURS)
None
} else if (item == Messages.dontShowAgain) {
tables.dismissedNotifications.ChangeScalafmtVersion
.dismissForever()
None
} else None
}
}
Expand All @@ -224,16 +220,15 @@ final class FormattingProvider(
if (
item == MissingScalafmtConf.createFile || item == MissingScalafmtConf.runDefaults
) {
val toWrite =
if (item == MissingScalafmtConf.createFile)
projectRoot.resolve(defaultScalafmtLocation)
else projectRoot.resolve(Directories.hiddenScalafmt)
Files
.write(
toWrite.toNIO,
initialConfig().getBytes(StandardCharsets.UTF_8),
)
client.showMessage(MissingScalafmtConf.fixedParams(isCancelled))
val relative =
if (item == MissingScalafmtConf.createFile) defaultScalafmtLocation
else Directories.hiddenScalafmt
val toWrite = projectRoot.resolve(relative)
Files.write(
toWrite.toNIO,
initialConfig().getBytes(StandardCharsets.UTF_8),
)
client.showMessage(MissingScalafmtConf.fixedParams(relative))
Some(toWrite)
} else if (item == Messages.notNow) {
tables.dismissedNotifications.CreateScalafmtFile
Expand Down
12 changes: 6 additions & 6 deletions metals/src/main/scala/scala/meta/internal/metals/Messages.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import org.eclipse.lsp4j.MessageActionItem
import org.eclipse.lsp4j.MessageParams
import org.eclipse.lsp4j.MessageType
import org.eclipse.lsp4j.ShowMessageRequestParams
import scala.meta.io.RelativePath

/**
* Constants for requests/dialogues via LSP window/showMessage and window/showMessageRequest.
Expand Down Expand Up @@ -615,20 +616,20 @@ object Messages {
else ""

def createFile = new MessageActionItem("Create .scalafmt.conf")
def runDefaults = new MessageActionItem("Run with defaults")
def runDefaults = new MessageActionItem("Run anyway")

def fixedParams(isAgain: Boolean): MessageParams =
def fixedParams(where: RelativePath): MessageParams =
new MessageParams(
MessageType.Info,
s"Created .scalafmt.conf${tryAgain(isAgain)}.",
s"Created $where.",
)

def isCreateScalafmtConf(params: ShowMessageRequestParams): Boolean =
params.getMessage == createScalafmtConfMessage

def createScalafmtConfMessage: String =
s"Unable to format since this workspace has no .scalafmt.conf file. " +
s"To fix this problem, create an empty .scalafmt.conf and try again."
s"No .scalafmt.conf file detected. " +
s"How would you like to proceed:"

def params(): ShowMessageRequestParams = {
val params = new ShowMessageRequestParams()
Expand All @@ -639,7 +640,6 @@ object Messages {
createFile,
runDefaults,
notNow,
dontShowAgain,
).asJava
)
params
Expand Down

0 comments on commit 29c5efe

Please sign in to comment.