Skip to content

Commit

Permalink
Added code style configuration.
Browse files Browse the repository at this point in the history
  • Loading branch information
dalgarins committed Jun 8, 2021
1 parent 325fd2c commit 419bd16
Show file tree
Hide file tree
Showing 11 changed files with 148 additions and 35 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ apply plugin: 'org.jetbrains.grammarkit'
import org.jetbrains.grammarkit.tasks.*

group 'co.anbora.labs'
version '2.5.8'
version '2.6.0'

repositories {
mavenCentral()
Expand Down
5 changes: 5 additions & 0 deletions src/main/html/change-notes.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
<br>
Plugin updates:
<ul>
<li><b>2.6.0</b> <em>(2021-06-07)</em> - Code Style</li>
<ul>
<li>Added code style configuration. </li>
<li>Fixed issue with code formatting. </li>
</ul>
<li><b>2.5.9</b> <em>(2021-05-27)</em> - Fixed minor issues - Android version</li>
<ul>
<li>Added ternary operator </li>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package co.anbora.labs.firebase.ide.color

import co.anbora.labs.firebase.ide.highlight.FirebaseSyntaxHighlighter
import co.anbora.labs.firebase.ide.icons.FirebaseIcons
import co.anbora.labs.firebase.lang.FirebaseRulesLanguage.LANGUAGE_DEMO_TEXT
import com.intellij.openapi.editor.colors.TextAttributesKey
import com.intellij.openapi.fileTypes.SyntaxHighlighter
import com.intellij.openapi.options.colors.AttributesDescriptor
Expand All @@ -23,37 +24,7 @@ class FirebaseColorSettingPage: ColorSettingsPage {

override fun getHighlighter(): SyntaxHighlighter = FirebaseSyntaxHighlighter()

override fun getDemoText(): String =
"""rules_version = '2';
service cloud.firestore {
// Allow the requestor to read or delete any resource on a path under the
// user directory.
match /users/{userId}/{anyUserFile=**} {
allow read, delete: if request.auth != null && request.auth.uid == userId;
}
match /databases/{database}/documents {
// True if the user is signed in or the requested data is 'public'
function signedInOrPublic() {
return request.auth.uid != null || resource.data.visibility == 'public';
}
match /{role}/{document=**} {
allow read, write: if
request.time < timestamp.date(2020, 9, 23) && role in request.auth.token.authorities;
}
}
// Allow the requestor to create or update their own images.
// When 'request.method' == 'delete' this rule and the one matching
// any path under the user directory would both match and the `delete`
// would be permitted.
match /users/{userId}/images/{imageId} {
// Whether to permit the request depends on the logical OR of all
// matched rules. This means that even if this rule did not explicitly
// allow the 'delete' the earlier rule would have.
allow write: if request.auth != null && request.auth.uid == userId && imageId.matches('*.png');
}
}"""
override fun getDemoText(): String = LANGUAGE_DEMO_TEXT

override fun getAdditionalHighlightingTagToDescriptorMap(): MutableMap<String, TextAttributesKey> = mutableMapOf()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package co.anbora.labs.firebase.ide.style

import co.anbora.labs.firebase.lang.FirebaseRulesLanguage
import com.intellij.application.options.CodeStyleAbstractConfigurable
import com.intellij.application.options.CodeStyleAbstractPanel
import com.intellij.psi.codeStyle.CodeStyleSettings

class FirebaseCodeStyleConfigurable(
settings: CodeStyleSettings,
originalSettings: CodeStyleSettings?
): CodeStyleAbstractConfigurable(settings, originalSettings, FirebaseRulesLanguage.LANGUAGE_NAME) {

override fun createPanel(
settings: CodeStyleSettings
): CodeStyleAbstractPanel = FirebaseCodeStyleMainPanel(currentSettings, settings)

override fun getHelpTopic(): String? = null
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package co.anbora.labs.firebase.ide.style

import co.anbora.labs.firebase.lang.FirebaseRulesLanguage
import com.intellij.application.options.TabbedLanguageCodeStylePanel
import com.intellij.psi.codeStyle.CodeStyleSettings

class FirebaseCodeStyleMainPanel(
currentSettings: CodeStyleSettings,
settings: CodeStyleSettings
): TabbedLanguageCodeStylePanel(FirebaseRulesLanguage, currentSettings, settings) {

override fun initTabs(settings: CodeStyleSettings?) {
addIndentOptionsTab(settings)
addSpacesTab(settings)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package co.anbora.labs.firebase.ide.style

import com.intellij.psi.codeStyle.CodeStyleSettings
import com.intellij.psi.codeStyle.CustomCodeStyleSettings

class FirebaseCodeStyleSettings(
container: CodeStyleSettings?
): CustomCodeStyleSettings("FirebaseCodeStyleSettings", container)
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package co.anbora.labs.firebase.ide.style

import co.anbora.labs.firebase.lang.FirebaseRulesLanguage.LANGUAGE_NAME
import com.intellij.openapi.options.Configurable
import com.intellij.psi.codeStyle.CodeStyleSettings
import com.intellij.psi.codeStyle.CodeStyleSettingsProvider
import com.intellij.psi.codeStyle.CustomCodeStyleSettings

object FirebaseCodeStyleSettingsProvider: CodeStyleSettingsProvider() {

override fun createCustomSettings(
settings: CodeStyleSettings?
): CustomCodeStyleSettings = FirebaseCodeStyleSettings(settings)

override fun getConfigurableDisplayName(): String = LANGUAGE_NAME

override fun createSettingsPage(
settings: CodeStyleSettings,
originalSettings: CodeStyleSettings?
): Configurable = FirebaseCodeStyleConfigurable(settings, originalSettings)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package co.anbora.labs.firebase.ide.style

import co.anbora.labs.firebase.lang.FirebaseRulesLanguage
import co.anbora.labs.firebase.lang.FirebaseRulesLanguage.LANGUAGE_DEMO_TEXT
import com.intellij.application.options.IndentOptionsEditor
import com.intellij.application.options.SmartIndentOptionsEditor
import com.intellij.lang.Language
import com.intellij.psi.codeStyle.CodeStyleSettingsCustomizable
import com.intellij.psi.codeStyle.CommonCodeStyleSettings
import com.intellij.psi.codeStyle.LanguageCodeStyleSettingsProvider

object FirebaseLanguageCodeStyleSettingProvider: LanguageCodeStyleSettingsProvider() {

override fun getLanguage(): Language = FirebaseRulesLanguage

override fun getCodeSample(settingsType: SettingsType): String = LANGUAGE_DEMO_TEXT

override fun customizeSettings(consumer: CodeStyleSettingsCustomizable, settingsType: SettingsType) {
if (settingsType == SettingsType.SPACING_SETTINGS) {
consumer.showStandardOptions("SPACE_AFTER_COMMA")
consumer.showStandardOptions("SPACE_BEFORE_COMMA")
} else if (settingsType == SettingsType.BLANK_LINES_SETTINGS) {
consumer.showStandardOptions("KEEP_BLANK_LINES_IN_CODE")
}
}

override fun customizeDefaults(commonSettings: CommonCodeStyleSettings, indentOptions: CommonCodeStyleSettings.IndentOptions) {
indentOptions.INDENT_SIZE = 4
commonSettings.KEEP_BLANK_LINES_IN_CODE = 0
}

override fun getIndentOptionsEditor(): IndentOptionsEditor = SmartIndentOptionsEditor()
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package co.anbora.labs.firebase.lang

import co.anbora.labs.firebase.ide.icons.FirebaseIcons
import co.anbora.labs.firebase.lang.FirebaseRulesLanguage.LANGUAGE_NAME
import com.intellij.openapi.fileTypes.LanguageFileType
import javax.swing.Icon

object FirebaseFileType: LanguageFileType(FirebaseRulesLanguage) {

const val EXTENSION = "rules"
private const val EXTENSION = "rules"

override fun getName(): String = "Firebase Rules"
override fun getName(): String = LANGUAGE_NAME

override fun getDescription(): String = "Firebase rules configurations"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,40 @@ package co.anbora.labs.firebase.lang

import com.intellij.lang.Language

object FirebaseRulesLanguage: Language("firebase_rules")
object FirebaseRulesLanguage: Language("firebase_rules") {

const val LANGUAGE_NAME = "Firebase Rules"

const val LANGUAGE_DEMO_TEXT =
"""rules_version = '2';
service cloud.firestore {
// Allow the requestor to read or delete any resource on a path under the
// user directory.
match /users/{userId}/{anyUserFile=**} {
allow read, delete: if request.auth != null && request.auth.uid == userId;
}
match /databases/{database}/documents {
// True if the user is signed in or the requested data is 'public'
function signedInOrPublic() {
return request.auth.uid != null || resource.data.visibility == 'public';
}
match /{role}/{document=**} {
allow read, write: if
request.time < timestamp.date(2020, 9, 23) && role in request.auth.token.authorities;
}
}
// Allow the requestor to create or update their own images.
// When 'request.method' == 'delete' this rule and the one matching
// any path under the user directory would both match and the `delete`
// would be permitted.
match /users/{userId}/images/{imageId} {
// Whether to permit the request depends on the logical OR of all
// matched rules. This means that even if this rule did not explicitly
// allow the 'delete' the earlier rule would have.
allow write: if request.auth != null && request.auth.uid == userId && imageId.matches('*.png');
}
}"""

}
4 changes: 4 additions & 0 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
<lang.foldingBuilder language="firebase_rules" implementationClass="co.anbora.labs.firebase.ide.folding.RuleFoldingBuilder"/>
<completion.contributor language="firebase_rules"
implementationClass="co.anbora.labs.firebase.lang.core.completion.FirebaseCompletionContributor"/>
<codeStyleSettingsProvider
implementation="co.anbora.labs.firebase.ide.style.FirebaseCodeStyleSettingsProvider"/>
<langCodeStyleSettingsProvider
implementation="co.anbora.labs.firebase.ide.style.FirebaseLanguageCodeStyleSettingProvider"/>
</extensions>

<actions>
Expand Down

0 comments on commit 419bd16

Please sign in to comment.