generated from JetBrains/intellij-platform-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add "format on save" for rider users
- Loading branch information
1 parent
40dd33a
commit 383ee2a
Showing
9 changed files
with
117 additions
and
1 deletion.
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
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
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
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
29 changes: 29 additions & 0 deletions
29
src/main/kotlin/com/github/aarcangeli/ideaclangformat/onsave/ClangFormatOnSave.kt
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,29 @@ | ||
package com.github.aarcangeli.ideaclangformat.onsave | ||
|
||
import com.github.aarcangeli.ideaclangformat.ClangFormatConfig | ||
import com.github.aarcangeli.ideaclangformat.services.ClangFormatService | ||
import com.github.aarcangeli.ideaclangformat.utils.ClangFormatCommons | ||
import com.intellij.ide.actionsOnSave.impl.ActionsOnSaveFileDocumentManagerListener.ActionOnSave | ||
import com.intellij.openapi.components.service | ||
import com.intellij.openapi.editor.Document | ||
import com.intellij.openapi.fileEditor.FileDocumentManager | ||
import com.intellij.openapi.project.Project | ||
import com.intellij.psi.PsiDocumentManager | ||
|
||
class ClangFormatOnSave : ActionOnSave() { | ||
override fun isEnabledForProject(project: Project): Boolean { | ||
return service<ClangFormatConfig>().state.formatOnSave && ClangFormatCommons.isUsingCustomFormatOnSave() | ||
} | ||
|
||
override fun processDocuments(project: Project, documents: Array<Document?>) { | ||
val fileDocumentManager = service<FileDocumentManager>() | ||
|
||
for (document in documents) { | ||
val virtualFile = fileDocumentManager.getFile(document ?: continue) ?: continue | ||
val psiFile = PsiDocumentManager.getInstance(project).getPsiFile(document) ?: continue | ||
if (service<ClangFormatService>().mayBeFormatted(psiFile, false)) { | ||
service<ClangFormatService>().reformatFileSync(project, virtualFile) | ||
} | ||
} | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/main/kotlin/com/github/aarcangeli/ideaclangformat/onsave/ClangFormatOnSaveActionInfo.kt
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,33 @@ | ||
// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. | ||
package com.github.aarcangeli.ideaclangformat.onsave | ||
|
||
import com.github.aarcangeli.ideaclangformat.ClangFormatConfig | ||
import com.intellij.ide.actionsOnSave.ActionOnSaveContext | ||
import com.intellij.ide.actionsOnSave.ActionOnSaveInfo | ||
import com.intellij.openapi.components.service | ||
|
||
|
||
class ClangFormatOnSaveActionInfo(context: ActionOnSaveContext) : ActionOnSaveInfo(context) { | ||
private val settings = service<ClangFormatConfig>().state | ||
private var isFormatOnSaveEnabled: Boolean = settings.formatOnSave | ||
|
||
override fun apply() { | ||
settings.formatOnSave = isFormatOnSaveEnabled | ||
} | ||
|
||
override fun isModified(): Boolean { | ||
return isFormatOnSaveEnabled != settings.formatOnSave | ||
} | ||
|
||
override fun getActionOnSaveName(): String { | ||
return "Run clang-format" | ||
} | ||
|
||
override fun isActionOnSaveEnabled(): Boolean { | ||
return isFormatOnSaveEnabled | ||
} | ||
|
||
override fun setActionOnSaveEnabled(enabled: Boolean) { | ||
isFormatOnSaveEnabled = enabled | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
...main/kotlin/com/github/aarcangeli/ideaclangformat/onsave/ClangFormatOnSaveInfoProvider.kt
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,15 @@ | ||
package com.github.aarcangeli.ideaclangformat.onsave | ||
|
||
import com.github.aarcangeli.ideaclangformat.utils.ClangFormatCommons | ||
import com.intellij.ide.actionsOnSave.ActionOnSaveContext | ||
import com.intellij.ide.actionsOnSave.ActionOnSaveInfo | ||
import com.intellij.ide.actionsOnSave.ActionOnSaveInfoProvider | ||
|
||
class ClangFormatOnSaveInfoProvider : ActionOnSaveInfoProvider() { | ||
override fun getActionOnSaveInfos(context: ActionOnSaveContext): Collection<ActionOnSaveInfo> { | ||
if (ClangFormatCommons.isUsingCustomFormatOnSave()) { | ||
return mutableListOf(ClangFormatOnSaveActionInfo(context)) | ||
} | ||
return emptyList() | ||
} | ||
} |
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
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