-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit ac7bc5f
Showing
14 changed files
with
480 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
.DS_Store | ||
.AppleDouble | ||
.LSOverride | ||
.idea | ||
out |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
style = defaultWithAlign | ||
maxColumn = 120 | ||
continuationIndent.callSite = 2 | ||
continuationIndent.defnSite = 2 | ||
assumeStandardLibraryStripMargin = true | ||
spaces.inImportCurlyBraces = false | ||
align.tokens = [ | ||
{ code = "<-", owner = "Enumerator.Generator" } | ||
{ code = "=>", owner = "Case" } | ||
{ code = "=" } | ||
{ code = "->", owner = "Term.ApplyInfix" } | ||
{ code = "," , owner = "Term" } | ||
{ code = "extends" , owner = "Term|Defn" } | ||
"//" | ||
] | ||
rewrite.rules = [RedundantParens, PreferCurlyFors] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2018 Chaerim Yeo | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# yapf | ||
[YAPF](https://github.com/google/yapf) plugin for Jetbrains IDEs. | ||
|
||
## Getting Started | ||
|
||
### Prerequisites | ||
- You should install [YAPF](https://github.com/google/yapf) before using this plugin. | ||
- You should know the path of YAPF executable. | ||
|
||
### Installing | ||
- Find `YAPF` in `Preferences` > `Plugins` > `Browse Repositories' on your Jetbrains IDE. | ||
- Install it! | ||
|
||
### Setting | ||
You can set following settings in `Preferences` > `YAPF`. | ||
- Format on save | ||
- YAPF executable path (default: `/usr/local/bin/yapf`) | ||
- YAPF style file name (default: `.style.yapf`) | ||
|
||
Note that this plugin passes style file to YAPF in the following order: | ||
1. `PROJECT_ROOT/[style_file_name]` | ||
2. `VERSION_CONTROL_SYSTEM_ROOT/[style_file_name]` | ||
3. `PROJECT_ROOT/.style.yapf` | ||
4. `VERSION_CONTROL_SYSTEM_ROOT/.style.yapf` | ||
5. No style option | ||
|
||
## TODO | ||
- Write unit tests | ||
|
||
## License | ||
This project is licensed under the MIT License. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<idea-plugin> | ||
<id>me.chaerim.yapf</id> | ||
<name>yapf</name> | ||
<version>0.1</version> | ||
<vendor email="[email protected]" url="https://github.com/cryeo">Chaerim Yeo</vendor> | ||
|
||
<description><![CDATA[ | ||
<a href="https://github.com/google/yapf">google/yapf</a> plugin for Jetbrains IDEs. | ||
]]> | ||
</description> | ||
|
||
<change-notes></change-notes> | ||
|
||
<!-- please see http://www.jetbrains.org/intellij/sdk/docs/basics/getting_started/build_number_ranges.html for description --> | ||
<idea-version since-build="145.0"/> | ||
|
||
<!-- please see http://www.jetbrains.org/intellij/sdk/docs/basics/getting_started/plugin_compatibility.html | ||
on how to target different products --> | ||
<depends>com.intellij.modules.lang</depends> | ||
<depends>com.intellij.modules.platform</depends> | ||
<depends>com.intellij.modules.vcs</depends> | ||
|
||
<extensions defaultExtensionNs="com.intellij"> | ||
<projectConfigurable groupId="tools" | ||
displayName="YAPF" | ||
id="preferences.yapf" | ||
instance="me.chaerim.yapf.SettingsConfigurable" /> | ||
<projectService serviceImplementation="me.chaerim.yapf.Settings"/> | ||
</extensions> | ||
|
||
<actions> | ||
<action id="YapfFormat" class="me.chaerim.yapf.FormatAction" text="Reformat code with YAPF" | ||
description="Reformat code with YAPF"> | ||
<add-to-group group-id="CodeMenu" anchor="first"/> | ||
<keyboard-shortcut keymap="$default" first-keystroke="meta alt SEMICOLON"/> | ||
</action> | ||
</actions> | ||
|
||
<application-components> | ||
<component> | ||
<implementation-class>me.chaerim.yapf.FormatOnSaveComponent</implementation-class> | ||
</component> | ||
</application-components> | ||
</idea-plugin> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package me.chaerim.yapf | ||
|
||
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.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.Util._ | ||
|
||
case class Document(document: IdeaDocument) { | ||
private val virtualFileOfDocument: Option[VirtualFile] = Option(FileDocumentManager.getInstance.getFile(document)) | ||
|
||
private val projectOfDocument: Option[Project] = virtualFileOfDocument.flatMap { virtualFile => | ||
ProjectManager.getInstance.getOpenProjects.find { project => | ||
ProjectRootManager.getInstance(project).getFileIndex.isInContent(virtualFile) | ||
} | ||
} | ||
|
||
val settings: Option[Settings] = projectOfDocument.map(Settings(_)) | ||
|
||
val isFormattable: Boolean = virtualFileOfDocument.exists { virtualFile => | ||
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 | ||
executable <- maybeExecutable | ||
if Files.exists(Paths.get(executable)) | ||
} yield executable).headOption.toRight(NotFoundExecutable) | ||
|
||
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 = | ||
(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 { | ||
case Right(formattedCode) => setFormattedCode(document, formattedCode) | ||
case Left(result) if result.shouldNotify => | ||
notifyMessage(s"${result.message}\n${result.detail.getOrElse("")}".stripMargin, NotificationType.ERROR) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package me.chaerim.yapf | ||
|
||
import com.intellij.openapi.actionSystem.{AnAction, AnActionEvent, CommonDataKeys} | ||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package me.chaerim.yapf | ||
|
||
import com.intellij.AppTopics | ||
import com.intellij.openapi.application.ApplicationManager | ||
import com.intellij.openapi.components.ApplicationComponent | ||
import com.intellij.openapi.editor.{Document => IdeaDocument} | ||
import com.intellij.openapi.fileEditor.FileDocumentManagerAdapter | ||
|
||
class FormatOnSaveComponent extends ApplicationComponent { | ||
override def getComponentName: String = s"${Settings.PluginName}.FormatOnSave" | ||
|
||
private val fileDocumentManagerAdapter: FileDocumentManagerAdapter = | ||
new FileDocumentManagerAdapter { | ||
override def beforeDocumentSaving(ideaDocument: IdeaDocument): Unit = | ||
for { | ||
document <- Option(Document(ideaDocument)) | ||
settings <- document.settings | ||
if settings.formatOnSave | ||
} yield document.format | ||
} | ||
|
||
override def initComponent(): Unit = | ||
ApplicationManager.getApplication.getMessageBus.connect | ||
.subscribe(AppTopics.FILE_DOCUMENT_SYNC, fileDocumentManagerAdapter) | ||
|
||
override def disposeComponent(): Unit = () | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package me.chaerim.yapf | ||
|
||
abstract class Result(val code: Int, | ||
val message: String, | ||
val detail: Option[String] = None, | ||
val shouldNotify: Boolean = true) | ||
|
||
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 class IllegalYapfResult(override val detail: Option[String]) | ||
extends Result(2000, "YAPF result is illegal", detail) | ||
case class FailedToRunCommand(override val detail: Option[String]) | ||
extends Result(3000, "Failed to run command", detail) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
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 | ||
|
||
@State(name = "YapfSettings", storages = Array(new Storage(StoragePathMacros.WORKSPACE_FILE))) | ||
class Settings extends PersistentStateComponent[Settings] { | ||
@BeanProperty | ||
var formatOnSave: Boolean = false | ||
|
||
@BeanProperty | ||
var executablePath: String = Settings.DefaultExecutablePath | ||
|
||
@BeanProperty | ||
var styleFileName: String = Settings.DefaultStyleFileName | ||
|
||
override def loadState(config: Settings): Unit = XmlSerializerUtil.copyBean(config, this) | ||
|
||
override def getState: Settings = this | ||
} | ||
|
||
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]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package me.chaerim.yapf | ||
|
||
import com.intellij.openapi.options.SearchableConfigurable | ||
import com.intellij.openapi.project.Project | ||
import javax.swing.JComponent | ||
|
||
class SettingsConfigurable(project: Project) extends SearchableConfigurable { | ||
private val settings: Settings = Settings(project) | ||
private val panel: SettingsPanel = new SettingsPanel(settings) | ||
|
||
override def getDisplayName: String = Settings.PluginName | ||
|
||
override def getId: String = s"preference.${Settings.PluginName.toLowerCase}" | ||
|
||
override def getHelpTopic: String = s"reference.settings.${Settings.PluginName.toLowerCase}" | ||
|
||
override def enableSearch(option: String): Runnable = super.enableSearch(option) | ||
|
||
override def createComponent(): JComponent = panel.createPanel | ||
|
||
override def isModified: Boolean = panel.isModified | ||
|
||
override def disposeUIResources(): Unit = super.disposeUIResources() | ||
|
||
override def apply(): Unit = panel.apply | ||
|
||
override def reset(): Unit = panel.reset | ||
} |
Oops, something went wrong.