Skip to content

Commit

Permalink
Fix problems
Browse files Browse the repository at this point in the history
  • Loading branch information
cryeo committed Sep 5, 2018
1 parent ac7bc5f commit 37c1b06
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 63 deletions.
2 changes: 1 addition & 1 deletion resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<idea-plugin>
<id>me.chaerim.yapf</id>
<name>yapf</name>
<version>0.1</version>
<version>0.2</version>
<vendor email="[email protected]" url="https://github.com/cryeo">Chaerim Yeo</vendor>

<description><![CDATA[
Expand Down
58 changes: 24 additions & 34 deletions src/me/chaerim/yapf/Document.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,60 +5,50 @@ import java.nio.file.{Files, Paths}
import com.intellij.notification.NotificationType
import com.intellij.openapi.editor.{Document => IdeaDocument}
import com.intellij.openapi.fileEditor.FileDocumentManager
import com.intellij.openapi.project.{Project, ProjectManager}
import com.intellij.openapi.project.{ProjectManager, Project => IdeaProject}
import com.intellij.openapi.roots.ProjectRootManager
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.vcsUtil.VcsUtil
import me.chaerim.yapf.Result.{AlreadyFormattedCode, NotFoundExecutable, UnformattableFile}
import me.chaerim.yapf.Project._
import me.chaerim.yapf.Result.NotFoundExecutable
import me.chaerim.yapf.Util._

case class Document(document: IdeaDocument) {
private val virtualFileOfDocument: Option[VirtualFile] = Option(FileDocumentManager.getInstance.getFile(document))
private val virtualFile: VirtualFile = FileDocumentManager.getInstance.getFile(document)

private val projectOfDocument: Option[Project] = virtualFileOfDocument.flatMap { virtualFile =>
ProjectManager.getInstance.getOpenProjects.find { project =>
ProjectRootManager.getInstance(project).getFileIndex.isInContent(virtualFile)
}
}
private val project: Option[IdeaProject] = ProjectManager.getInstance.getOpenProjects
.find(ProjectRootManager.getInstance(_).getFileIndex.isInContent(virtualFile))

val settings: Option[Settings] = projectOfDocument.map(Settings(_))
val projectSettings: Option[Settings] = project.map(_.settings)

val isFormattable: Boolean = virtualFileOfDocument.exists { virtualFile =>
virtualFile.getFileType.getName.compareToIgnoreCase("Python") == 0 &&
val isFormattable: Boolean = virtualFile.getFileType.getName.compareToIgnoreCase("Python") == 0 &&
virtualFile.getExtension.compareToIgnoreCase("py") == 0
}

private def findExecutable: Either[Result, String] =
(for {
maybeExecutable <- List(settings.map(_.executablePath), Option(Settings.DefaultExecutablePath)).distinct
maybeExecutable <- List(projectSettings.map(_.executablePath), Option(Settings.DefaultExecutablePath)).distinct
executable <- maybeExecutable
if Files.exists(Paths.get(executable))
} yield executable).headOption.toRight(NotFoundExecutable)

private def findConfigFile: Option[String] = {
private def findConfigFile: Option[String] =
(for {
project <- projectOfDocument.toList
maybeConfigFile <- List(settings.map(_.styleFileName), Option(Settings.DefaultStyleFileName)).distinct
maybeDirectory <- List(Option(project.getBasePath),
virtualFileOfDocument.map(VcsUtil.getVcsRootFor(project, _).getPath)).distinct
directory <- maybeDirectory
configFile <- maybeConfigFile
fullConfigFilePath = Paths.get(directory, configFile)
if Files.exists(fullConfigFilePath)
} yield fullConfigFilePath.toAbsolutePath.toString).headOption
}

def format: Unit =
maybeConfigFile <- List(projectSettings.map(_.styleFileName), Option(Settings.DefaultStyleFileName)).distinct
maybeDirectory <- List(project.map(_.getBasePath), project.map(VcsUtil.getVcsRootFor(_, virtualFile).getPath)).distinct
directory <- maybeDirectory
configFile <- maybeConfigFile
fullPath = Paths.get(directory, configFile)
if Files.exists(fullPath)
} yield fullPath.toAbsolutePath.toString).headOption

def format: Unit = if (isFormattable) {
(for {
executable <- findExecutable
_ <- Either.cond(isFormattable, "", UnformattableFile)
configFile = findConfigFile
originalCode = document.getText
formattedCode <- runYapfCommand(executable, originalCode, configFile)
result <- Either.cond(originalCode != formattedCode, formattedCode, AlreadyFormattedCode)
} yield result) match {
executable <- findExecutable
formattedCode <- runYapfCommand(executable, document.getText, findConfigFile)
} yield formattedCode) match {
case Right(formattedCode) => setFormattedCode(document, formattedCode)
case Left(result) if result.shouldNotify =>
case Left(result) =>
notifyMessage(s"${result.message}\n${result.detail.getOrElse("")}".stripMargin, NotificationType.ERROR)
}
}
}
8 changes: 3 additions & 5 deletions src/me/chaerim/yapf/FormatAction.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ import com.intellij.openapi.fileEditor.FileEditorManager
class FormatAction extends AnAction {
override def actionPerformed(event: AnActionEvent): Unit =
for {
currentProject <- Option(event.getData(CommonDataKeys.PROJECT))
currentEditor <- Option(FileEditorManager.getInstance(currentProject).getSelectedTextEditor)
currentDocument <- Option(currentEditor.getDocument)
document <- Option(Document(currentDocument))
} yield document.format
currentProject <- Option(event.getData(CommonDataKeys.PROJECT))
currentEditor <- Option(FileEditorManager.getInstance(currentProject).getSelectedTextEditor)
} yield Document(currentEditor.getDocument).format
}
19 changes: 10 additions & 9 deletions src/me/chaerim/yapf/FormatOnSaveComponent.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,20 @@ import com.intellij.openapi.fileEditor.FileDocumentManagerAdapter
class FormatOnSaveComponent extends ApplicationComponent {
override def getComponentName: String = s"${Settings.PluginName}.FormatOnSave"

private val fileDocumentManagerAdapter: FileDocumentManagerAdapter =
override def initComponent(): Unit =
ApplicationManager.getApplication.getMessageBus.connect
.subscribe(AppTopics.FILE_DOCUMENT_SYNC, FormatOnSaveComponent.FileDocumentManagerAdapter)

override def disposeComponent(): Unit = ()
}

object FormatOnSaveComponent {
val FileDocumentManagerAdapter: FileDocumentManagerAdapter =
new FileDocumentManagerAdapter {
override def beforeDocumentSaving(ideaDocument: IdeaDocument): Unit =
for {
document <- Option(Document(ideaDocument))
settings <- document.settings
if settings.formatOnSave
if document.projectSettings.exists(_.formatOnSave)
} yield document.format
}

override def initComponent(): Unit =
ApplicationManager.getApplication.getMessageBus.connect
.subscribe(AppTopics.FILE_DOCUMENT_SYNC, fileDocumentManagerAdapter)

override def disposeComponent(): Unit = ()
}
10 changes: 10 additions & 0 deletions src/me/chaerim/yapf/Project.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package me.chaerim.yapf

import com.intellij.openapi.components.ServiceManager
import com.intellij.openapi.project.{Project => IdeaProject}

object Project {
implicit class RichProject(project: IdeaProject) {
val settings: Settings = ServiceManager.getService(project, classOf[Settings])
}
}
9 changes: 2 additions & 7 deletions src/me/chaerim/yapf/Result.scala
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
package me.chaerim.yapf

abstract class Result(val code: Int,
val message: String,
val detail: Option[String] = None,
val shouldNotify: Boolean = true)
abstract class Result(val code: Int, val message: String, val detail: Option[String] = None)

object Result {
case object NotFoundExecutable extends Result(1000, "YAPF executable is not found")
case object UnformattableFile extends Result(1001, "File is unformattable")
case object AlreadyFormattedCode extends Result(1002, "Code is already formatted", shouldNotify = false)
case object NotFoundExecutable extends Result(1000, "YAPF executable is not found")

case class IllegalYapfResult(override val detail: Option[String])
extends Result(2000, "YAPF result is illegal", detail)
Expand Down
3 changes: 0 additions & 3 deletions src/me/chaerim/yapf/Settings.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package me.chaerim.yapf

import com.intellij.openapi.components._
import com.intellij.openapi.project.Project
import com.intellij.util.xmlb.XmlSerializerUtil

import scala.beans.BeanProperty
Expand All @@ -26,6 +25,4 @@ object Settings {
val PluginName: String = "YAPF"
val DefaultStyleFileName: String = ".style.yapf"
val DefaultExecutablePath: String = "/usr/local/bin/yapf"

def apply(project: Project): Settings = ServiceManager.getService(project, classOf[Settings])
}
8 changes: 4 additions & 4 deletions src/me/chaerim/yapf/SettingsConfigurable.scala
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package me.chaerim.yapf

import com.intellij.openapi.options.SearchableConfigurable
import com.intellij.openapi.project.Project
import com.intellij.openapi.project.{Project => IdeaProject}
import javax.swing.JComponent
import me.chaerim.yapf.Project._

class SettingsConfigurable(project: Project) extends SearchableConfigurable {
private val settings: Settings = Settings(project)
private val panel: SettingsPanel = new SettingsPanel(settings)
class SettingsConfigurable(project: IdeaProject) extends SearchableConfigurable {
private val panel: SettingsPanel = new SettingsPanel(project.settings)

override def getDisplayName: String = Settings.PluginName

Expand Down

0 comments on commit 37c1b06

Please sign in to comment.