Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: Custom template variables provided by other plugins #280

Open
wants to merge 4 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ sealed class Data {
override fun toString(): String = "Data.None"
}

open class Idle (val idleTimestamp: Long) : Data() {
open class Idle(val idleTimestamp: Long) : Data() {
override fun toString(): String {
return "Data.Idle(idleTimestamp=$idleTimestamp)"
}
Expand All @@ -41,7 +41,8 @@ sealed class Data {
val applicationVersion: String,
val applicationTimeOpened: Long,
val applicationTimeActive: Long,
val applicationSettings: ApplicationSettings
val applicationSettings: ApplicationSettings,
val customVariableData: CustomVariableData,
) : Data() {
override fun toString(): String {
return "Data.Application(applicationVersion='$applicationVersion', applicationTimeOpened=$applicationTimeOpened, applicationTimeActive=$applicationTimeActive, applicationSettings=$applicationSettings)"
Expand All @@ -54,14 +55,15 @@ sealed class Data {
applicationTimeOpened: Long,
applicationTimeActive: Long,
applicationSettings: ApplicationSettings,
customVariableData: CustomVariableData,
val projectName: String,
val projectDescription: String,
val projectTimeOpened: Long,
val projectTimeActive: Long,
val projectSettings: ProjectSettings,
val vcsBranch: String?,
val debuggerActive: Boolean
) : Application(applicationName, applicationVersion, applicationTimeOpened, applicationTimeActive, applicationSettings) {
) : Application(applicationName, applicationVersion, applicationTimeOpened, applicationTimeActive, applicationSettings, customVariableData) {
override fun toString(): String {
return "Data.Project(applicationName='$applicationName', applicationVersion='$applicationVersion', applicationTimeOpened=$applicationTimeOpened, applicationTimeActive=$applicationTimeActive, projectName='$projectName', projectDescription='$projectDescription', projectTimeOpened=$projectTimeOpened, projectTimeActive=$projectTimeActive, vcsBranch=$vcsBranch)"
}
Expand All @@ -73,6 +75,7 @@ sealed class Data {
applicationTimeOpened: Long,
applicationTimeActive: Long,
applicationSettings: ApplicationSettings,
customVariableData: CustomVariableData,
projectName: String,
projectDescription: String,
projectTimeOpened: Long,
Expand All @@ -98,6 +101,7 @@ sealed class Data {
applicationTimeOpened,
applicationTimeActive,
applicationSettings,
customVariableData,
projectName,
projectDescription,
projectTimeOpened,
Expand Down Expand Up @@ -135,3 +139,17 @@ sealed class Data {
}
}
}

class CustomVariableData {
private val variables: MutableMap<String, String> = mutableMapOf()

operator fun set(name: String, value: String) {
variables[name] = value
}

operator fun get(name: String) = variables[name]

fun unset(name: String) {
variables.remove(name)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.almightyalpaca.jetbrains.plugins.discord.plugin.data

import com.almightyalpaca.jetbrains.plugins.discord.plugin.DiscordPlugin
import com.almightyalpaca.jetbrains.plugins.discord.plugin.extensions.CustomVariableProvider
import com.almightyalpaca.jetbrains.plugins.discord.plugin.extensions.VcsInfoExtension
import com.almightyalpaca.jetbrains.plugins.discord.plugin.render.Renderer
import com.almightyalpaca.jetbrains.plugins.discord.plugin.settings.settings
Expand Down Expand Up @@ -145,6 +146,12 @@ class DataService {

val vcsBranch = VcsInfoExtension.getCurrentVcsBranch(project, file)

val customVariableData = CustomVariableData()
CustomVariableProvider.forApplication(customVariableData)
CustomVariableProvider.forProject(customVariableData, project)
CustomVariableProvider.forFile(customVariableData, project, editor, file)


DiscordPlugin.LOG.debug("Returning file data")

return Data.File(
Expand All @@ -153,6 +160,7 @@ class DataService {
applicationTimeOpened,
applicationTimeActive,
applicationSettings,
customVariableData,
projectName,
projectDescription,
projectTimeOpened,
Expand All @@ -178,6 +186,10 @@ class DataService {

val vcsBranch = VcsInfoExtension.getCurrentVcsBranch(project, null)

val customVariableData = CustomVariableData()
CustomVariableProvider.forApplication(customVariableData)
CustomVariableProvider.forProject(customVariableData, project)

DiscordPlugin.LOG.debug("Returning project data")

return Data.Project(
Expand All @@ -186,6 +198,7 @@ class DataService {
applicationTimeOpened,
applicationTimeActive,
applicationSettings,
customVariableData,
projectName,
projectDescription,
projectTimeOpened,
Expand All @@ -197,14 +210,18 @@ class DataService {
}
}

val customVariableData = CustomVariableData()
CustomVariableProvider.forApplication(customVariableData)

DiscordPlugin.LOG.debug("Returning application data")

return Data.Application(
applicationName,
applicationVersion,
applicationTimeOpened,
applicationTimeActive,
applicationSettings
applicationSettings,
customVariableData,
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright 2017-2020 Aljoscha Grebe
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.almightyalpaca.jetbrains.plugins.discord.plugin.extensions

import com.almightyalpaca.jetbrains.plugins.discord.plugin.data.CustomVariableData
import com.intellij.openapi.extensions.ExtensionPointName
import com.intellij.openapi.fileEditor.FileEditor
import com.intellij.openapi.project.Project
import com.intellij.openapi.vfs.VirtualFile

interface CustomVariableProvider {
fun forApplication(variableData: CustomVariableData) {}
fun forProject(variableData: CustomVariableData, project: Project) {}
fun forFile(variableData: CustomVariableData, project: Project, editor: FileEditor, file: VirtualFile) {}

companion object {
private val EXTENSION_POINT_NAME = ExtensionPointName.create<CustomVariableProvider>("com.almightyalpaca.intellij.plugins.discord.customVariableProvider")

fun forApplication(variableData: CustomVariableData) {
EXTENSION_POINT_NAME.extensions.forEach { it.forApplication(variableData) }
}

fun forProject(variableData: CustomVariableData, project: Project) {
EXTENSION_POINT_NAME.extensions.forEach { it.forProject(variableData, project) }
}

fun forFile(variableData: CustomVariableData, project: Project, editor: FileEditor, file: VirtualFile) {
EXTENSION_POINT_NAME.extensions.forEach { it.forFile(variableData, project, editor, file) }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.almightyalpaca.jetbrains.plugins.discord.plugin.render.templates

import com.almightyalpaca.jetbrains.plugins.discord.plugin.data.CustomVariableData
import com.almightyalpaca.jetbrains.plugins.discord.plugin.data.Data
import com.almightyalpaca.jetbrains.plugins.discord.plugin.render.RenderContext
import com.almightyalpaca.jetbrains.plugins.discord.plugin.render.templates.antlr.TemplateLexer
Expand Down Expand Up @@ -47,7 +48,10 @@ object Utils {
"IsTextEditor" -> context.fileData?.editorIsTextEditor.toString()
"FileIsWritable" -> context.fileData?.fileIsWriteable.toString()
"DebuggerActive" -> context.projectData?.debuggerActive.toString()
else -> null
else -> {
// application data has all the custom variables
context.applicationData?.customVariableData?.get(varName)
}
}

private fun sizeAsString(size: Int?): String? {
Expand Down Expand Up @@ -84,6 +88,7 @@ object Utils {
is TemplateParser.VarContext -> {
getVarValue(child.NAME()?.text ?: "", context) ?: ""
}

is TemplateParser.FunContext -> {
val name = child.NAME()?.symbol?.text ?: ""
val req = when (name) {
Expand All @@ -100,33 +105,39 @@ object Utils {
"RegexEscape" -> {
Regex.escape(evalVisitor(context, arr[0]))
}

"NotNull" -> {
varNullCheck(context, evalVisitor(context, arr[0]))
.toString()
}

"Matches" -> {
withRegex(evalVisitor(context, arr[1])) {
evalVisitor(context, arr[0])
.matches(it)
.toString()
}
}

"ReplaceFirst" -> {
withRegex(evalVisitor(context, arr[1])) {
evalVisitor(context, arr[0])
.replaceFirst(it, evalVisitor(context, arr[2]))
}
}

"ReplaceAll" -> {
withRegex(evalVisitor(context, arr[1])) {
evalVisitor(context, arr[0])
.replace(it, evalVisitor(context, arr[2]))
}
}

else -> ""
}
}
}

is TemplateParser.If_ruleContext -> {
val args = child.text_eval()

Expand All @@ -150,11 +161,13 @@ object Utils {
}
}
}

is TemplateParser.Raw_text_ruleContext -> {
val txt = child.text
txt.substring(2, txt.length - 2) // take out the first and last 2 characters(the '#"' at the beginning
// and '"#' at the end)
}

else -> child.text // NAME/TEXT/parentheses from the grammar
}
}
Expand Down Expand Up @@ -193,6 +206,7 @@ private fun Data.asTemplateData(): TemplateData =
is Data.File ->
TemplateData.File(
this.applicationVersion,
this.customVariableData,
this.projectName,
this.projectDescription,
this.vcsBranch,
Expand All @@ -208,34 +222,40 @@ private fun Data.asTemplateData(): TemplateData =
this.pathInModule,
this.fileSize
)

is Data.Project ->
TemplateData.Project(
this.applicationVersion, this.projectName, this.projectDescription, this.vcsBranch, this.debuggerActive
this.applicationVersion, this.customVariableData, this.projectName, this.projectDescription, this.vcsBranch, this.debuggerActive
)

is Data.Application ->
TemplateData.Application(
this.applicationVersion
this.applicationVersion, this.customVariableData
)

else -> throw IllegalArgumentException()
}

fun RenderContext.asCustomTemplateContext() = CustomTemplateContext.from(this)

sealed class TemplateData {
open class Application(
val applicationVersion: String
val applicationVersion: String,
val customVariableData: CustomVariableData,
) : TemplateData()

open class Project(
applicationVersion: String,
customVariableData: CustomVariableData,
val projectName: String,
val projectDescription: String,
val vcsBranch: String?,
val debuggerActive: Boolean
) : Application(applicationVersion)
) : Application(applicationVersion, customVariableData)

open class File(
applicationVersion: String,
customVariableData: CustomVariableData,
projectName: String,
projectDescription: String,
vcsBranch: String?,
Expand All @@ -253,6 +273,7 @@ sealed class TemplateData {
val fileSize: Int
) : Project(
applicationVersion,
customVariableData,
projectName,
projectDescription,
vcsBranch,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.almightyalpaca.jetbrains.plugins.discord.plugin.settings.gui.preview

import com.almightyalpaca.jetbrains.plugins.discord.plugin.data.CustomVariableData
import com.almightyalpaca.jetbrains.plugins.discord.plugin.data.Data
import com.almightyalpaca.jetbrains.plugins.discord.plugin.data.dataService
import com.almightyalpaca.jetbrains.plugins.discord.plugin.render.RenderContext
Expand Down Expand Up @@ -487,6 +488,7 @@ private fun Data.completeMissingData(): Data.File {
applicationTimeOpened,
applicationTimeActive,
application?.applicationSettings ?: settings,
CustomVariableData(),
project?.projectName ?: "Dummy project",
if (projectDescription.isNullOrBlank()) "Dummies are very nice test objects" else projectDescription,
project?.projectTimeOpened ?: applicationTimeOpened,
Expand Down
1 change: 1 addition & 0 deletions plugin/src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -100,5 +100,6 @@

<extensionPoints>
<extensionPoint name="vcsInfo" interface="com.almightyalpaca.jetbrains.plugins.discord.plugin.extensions.VcsInfoExtension" dynamic="true"/>
<extensionPoint name="customVariableProvider" interface="com.almightyalpaca.jetbrains.plugins.discord.plugin.extensions.CustomVariableProvider" dynamic="true"/>
</extensionPoints>
</idea-plugin>
Loading