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

Develop #18

Merged
merged 15 commits into from
Jan 1, 2024
Merged
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
1 change: 1 addition & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.github.lauvsong.languagecursor

import com.github.lauvsong.languagecursor.listeners.NotEnglishKeyListener
import com.intellij.openapi.editor.EditorFactory
import com.intellij.openapi.project.Project
import com.intellij.openapi.startup.StartupActivity

class ProjectOpenStartUpActivity : StartupActivity.DumbAware {

override fun runActivity(project: Project) {
val editorFactory = EditorFactory.getInstance()
val listener = NotEnglishKeyListener()
editorFactory.eventMulticaster.addCaretListener(listener, project)
}

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.github.lauvsong.languagecursor.listeners

import com.github.lauvsong.languagecursor.settings.AppSettingsState
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.editor.colors.EditorColors
import com.intellij.openapi.editor.colors.EditorColorsManager
import com.intellij.openapi.editor.event.CaretEvent
import com.intellij.openapi.editor.event.CaretListener
import com.intellij.ui.JBColor
import java.awt.Color
import java.awt.im.InputContext
import java.util.Locale

class NotEnglishKeyListener : CaretListener {

private val originalCursorColor: Color = EditorColorsManager.getInstance()
.globalScheme
.getColor(EditorColors.CARET_COLOR)
?: JBColor.BLACK

override fun caretPositionChanged(event: CaretEvent) {
val editor = event.editor
updateCursorColor(editor)
}

private fun updateCursorColor(editor: Editor) {
val settings = AppSettingsState.instance
val isEnglishInput = isEnglishInput()
val cursorColor = if (!isEnglishInput) settings.cursorColor else editor.colorsScheme.defaultForeground

if (isEnglishInput) {
restoreOriginalCursorColor(editor)
} else {
noEnglishCursorColor(editor, cursorColor)
}
}

private fun isEnglishInput() : Boolean {
val locale = InputContext.getInstance().locale
val language = locale.language
val country = locale.country

// Explanation for not solely relying on constant `Locale.ENGLISH`:
// In some Locale configurations, the language code might not be explicitly provided.
// e.g., "_US_UserDefined_252"
// Therefore, Locales with country codes are also considered as English input.
// Canada (CA) is not included here because both English and French are used in Canada.
// ** If you encounter any edge cases, please feel free to open an issue on GitHub. **
if (language == Locale.ENGLISH.language) {
return true
}

if (country == Locale.US.country
|| country == Locale.UK.country) {
return true
}

return false
}

private fun noEnglishCursorColor(editor: Editor, newColor: Color) {
val scheme = editor.colorsScheme
scheme.setColor(EditorColors.CARET_COLOR, newColor)
}

private fun restoreOriginalCursorColor(editor: Editor) {
val scheme = editor.colorsScheme
scheme.setColor(EditorColors.CARET_COLOR, originalCursorColor)
}

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.github.lauvsong.languagecursor.settings

import com.intellij.ui.ColorPanel
import com.intellij.ui.components.JBLabel
import com.intellij.util.ui.FormBuilder
import javax.swing.JPanel

class AppSettingsComponent {
val mainPanel: JPanel by lazy {
FormBuilder.createFormBuilder()
.addLabeledComponent(JBLabel("Cursor color: "), cursorColor, 1, false)
.addComponentFillVertically(JPanel(), 0)
.panel
}

val cursorColor: ColorPanel = ColorPanel()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.github.lauvsong.languagecursor.settings

import com.intellij.openapi.options.Configurable
import javax.swing.JComponent

class AppSettingsConfigurable : Configurable {

private lateinit var settingsComponent : AppSettingsComponent

override fun getDisplayName(): String =
"Language Cursor"

override fun createComponent(): JComponent? {

Check warning on line 13 in src/main/kotlin/com/github/lauvsong/languagecursor/settings/AppSettingsConfigurable.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Redundant nullable return type

'createComponent' always returns non-null type
settingsComponent = AppSettingsComponent()
return settingsComponent.mainPanel
}

override fun isModified(): Boolean {
val settings = AppSettingsState.instance
return settings.cursorColor != settingsComponent.cursorColor
}

override fun apply() {
val settings = AppSettingsState.instance
settings.cursorColorAsRgb = settingsComponent.cursorColor.selectedColor?.rgb ?: settings.cursorColorAsRgb
}

override fun reset() {
val settings = AppSettingsState.instance
settingsComponent.cursorColor.selectedColor = settings.cursorColor
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.github.lauvsong.languagecursor.settings

import com.intellij.openapi.components.PersistentStateComponent
import com.intellij.openapi.components.State
import com.intellij.openapi.components.Storage
import com.intellij.openapi.components.service
import com.intellij.ui.JBColor
import com.intellij.util.xmlb.XmlSerializerUtil
import java.awt.Color

@State(
name = "com.github.lauvsong.languagecursor.settings.AppSettingsState",
storages = [Storage("AppSettingsState.xml")]
)
class AppSettingsState : PersistentStateComponent<AppSettingsState> {
var cursorColorAsRgb: Int = JBColor.RED.rgb

val cursorColor: Color
get() = Color(cursorColorAsRgb)

override fun getState(): AppSettingsState {
return this
}

override fun loadState(state: AppSettingsState) {
XmlSerializerUtil.copyBean(state, this)
}

companion object {
val instance: AppSettingsState
get() = service()
}
}

This file was deleted.

15 changes: 9 additions & 6 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
<!-- Plugin Configuration File. Read more: https://plugins.jetbrains.com/docs/intellij/plugin-configuration-file.html -->
<idea-plugin>
<id>com.github.lauvsong.languagecursor</id>
<name>language-cursor Template</name>
<name>Language Cursor</name>
<vendor>lauvsong</vendor>

<depends>com.intellij.modules.platform</depends>

<resource-bundle>messages.MyBundle</resource-bundle>

<extensions defaultExtensionNs="com.intellij">
<toolWindow factoryClass="com.github.lauvsong.languagecursor.toolWindow.MyToolWindowFactory" id="MyToolWindow"/>
<applicationConfigurable
parentId="tools"
instance="com.github.lauvsong.languagecursor.settings.AppSettingsConfigurable"
id="com.github.lauvsong.languagecursor.settings.AppSettingsConfigurable"
displayName="Language Cursor"
/>
<applicationService serviceImplementation="com.github.lauvsong.languagecursor.settings.AppSettingsState"/>
<postStartupActivity implementation="com.github.lauvsong.languagecursor.ProjectOpenStartUpActivity"/>
</extensions>

<applicationListeners>
<listener class="com.github.lauvsong.languagecursor.listeners.MyApplicationActivationListener" topic="com.intellij.openapi.application.ApplicationActivationListener"/>
</applicationListeners>
</idea-plugin>
39 changes: 0 additions & 39 deletions src/test/kotlin/com/github/lauvsong/languagecursor/MyPluginTest.kt

This file was deleted.

3 changes: 0 additions & 3 deletions src/test/testData/rename/foo.xml

This file was deleted.

3 changes: 0 additions & 3 deletions src/test/testData/rename/foo_after.xml

This file was deleted.

Loading