Skip to content

Instantly share code, notes, and snippets.

View rubenquadros's full-sized avatar

Ruben Quadros rubenquadros

  • Bangalore
View GitHub Profile
@rubenquadros
rubenquadros / InspectionSettings.kt
Last active July 21, 2022 20:19
Inspection setting configurable
class InspectionSettings(project: Project) : Configurable {
private var component: InspectionComponent? = null
private val inspectionSettingState = project.service<InspectionSettingState>()
override fun createComponent(): JComponent? {
component = InspectionComponent(inspectionSettingState)
return component?.getPanel()
}
class InspectionComponent(
private val inspectionSettingState: InspectionSettingState
) {
// used to maintain the current state
private var parser: Parser = Parser.GSON
init {
// set the current state to the persisted state
parser = inspectionSettingState.state.parser
@rubenquadros
rubenquadros / project_settings_service.xml
Last active July 21, 2022 18:33
Declaring the service in plugin.xml
<extensions defaultExtensionNs="com.intellij">
<!-- other configurations -->
<projectService
serviceImplementation="com.ruben.codespector.settings.InspectionSettingState" />
</extensions>
@rubenquadros
rubenquadros / InspectionSettingState.kt
Created July 21, 2022 18:26
Persisting the plugin settings state
@State(
name = "com.ruben.codespector.settings.InspectionSettingState",
storages = [Storage("Codespector.xml")]
)
class InspectionSettingState :
PersistentStateComponent<InspectionSettingState> {
var parser: Parser = Parser.GSON
override fun getState(): InspectionSettingState {
@rubenquadros
rubenquadros / InitialComponent.kt
Last active July 21, 2022 19:23
Initial settings UI
class InspectionComponent {
fun getPanel(): JPanel = panel {
titledRow("Configure Plugin Settings For Your Project") {
row("Select JSON parser:") {
buttonGroup {
row {
radioButton(
text = "Gson"
)
@rubenquadros
rubenquadros / project_settings.xml
Last active July 21, 2022 18:28
Project level settings of Codespector
<extensions defaultExtensionNs="com.intellij">
<!-- other configurations -->
<projectConfigurable
parentId="editor"
displayName="Codespector Settings"
nonDefaultProject="true"
instance="com.ruben.codespector.settings.InspectionSettings"
id="com.ruben.codespector.settings.InspectionSettings" />
@rubenquadros
rubenquadros / DefaultExceptionhandler.kt
Created June 12, 2022 15:14
Default exception handler for all unhandled exceptions
//////////////////////////////////////////////
// Activity code
//////////////////////////////////////////////
val exceptionHandler =
Thread.UncaughtExceptionHandler { _: Thread, e: Throwable ->
handleUncaughtException(e)
}
private fun attachUnhandledExceptionHandler() {
@rubenquadros
rubenquadros / HomeViewModel.kt
Created June 12, 2022 14:50
Viewmodel to perform work
class HomeViewModel : ViewModel() {
fun handleException() {
viewModelScope.launch { runCatching { doWork() } }
}
fun unhandledException() {
viewModelScope.launch { doWork() }
}
@rubenquadros
rubenquadros / HomeScreen.kt
Created June 12, 2022 14:48
App home screen
@Composable
fun HomeScreen(handleException: () -> Unit, unhandledException: () -> Unit) {
Column(modifier = Modifier.fillMaxSize()) {
Button(
modifier = Modifier.align(Alignment.CenterHorizontally),
onClick = handleException
) { Text(text = "Handle exception") }
Button(
modifier = Modifier.align(Alignment.CenterHorizontally),
@rubenquadros
rubenquadros / KtClassExtensions.kt
Last active May 3, 2022 09:06
An extension function on KtClass to see if any param is missing SerializedName annotation
// returns the param if SerializedName annotation is missing
fun KtClass.getMissingAnnotationParam(): KtParameter? {
this.getPrimaryConstructorParameterList()?.parameters?.forEach { param ->
if (!param.annotationEntries.any {
it.shortName?.asString() == "SerializedName"
}
) {
return param
}
}