-
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.
Merge pull request #3 from Cullen-Shannon/add-file-service
Added the `FileInputService` to read from & write back to the config file
- Loading branch information
Showing
12 changed files
with
326 additions
and
78 deletions.
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
11 changes: 0 additions & 11 deletions
11
src/main/java/org/jetbrains/plugins/template/MyMenuItem.kt
This file was deleted.
Oops, something went wrong.
12 changes: 12 additions & 0 deletions
12
src/main/java/org/jetbrains/plugins/template/domain/FilePluginState.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,12 @@ | ||
package org.jetbrains.plugins.template.domain | ||
|
||
/* | ||
Stores the state for the currently installed plugin | ||
`fileName`: the name of the file that can be configured by the user within `AppSettingsConfigurable` | ||
`currentMenuItemConfig`: `MyMenuItem` corresponding to the existing menu item data that was previously read in from the user's config file | ||
*/ | ||
data class FilePluginState( | ||
var fileName: String = "config.json", | ||
var currentMenuItemConfig: MyMenuItem? = null | ||
) |
11 changes: 11 additions & 0 deletions
11
src/main/java/org/jetbrains/plugins/template/domain/MyMenuItem.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,11 @@ | ||
package org.jetbrains.plugins.template.domain | ||
|
||
data class MyMenuItem( | ||
var text: String, | ||
var description: String, | ||
var url: String? = null, | ||
var children: List<MyMenuItem>? = null, | ||
var addSeparatorBefore: Boolean = false, | ||
var addSeparatorAfter: Boolean = false, | ||
var isTopLevel: Boolean = false | ||
) |
74 changes: 74 additions & 0 deletions
74
src/main/java/org/jetbrains/plugins/template/services/FileInputService.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,74 @@ | ||
package org.jetbrains.plugins.template.services | ||
|
||
import com.google.gson.Gson | ||
import com.intellij.openapi.command.WriteCommandAction | ||
import com.intellij.openapi.components.Service | ||
import com.intellij.openapi.fileEditor.impl.LoadTextUtil | ||
import com.intellij.openapi.project.Project | ||
import com.intellij.openapi.project.ProjectManager | ||
import com.intellij.openapi.vfs.VfsUtil | ||
import com.intellij.openapi.vfs.VirtualFile | ||
import com.intellij.psi.search.FilenameIndex | ||
import com.intellij.psi.search.GlobalSearchScope | ||
import org.jetbrains.plugins.template.domain.MyMenuItem | ||
import java.io.IOException | ||
|
||
@Service(Service.Level.PROJECT) | ||
class FileInputService { | ||
|
||
private val project = ProjectManager.getInstance().openProjects.first() | ||
private val pluginSettingsService = PluginSettingsService.getInstance(project) | ||
|
||
fun readConfigFileContents(): MyMenuItem? { | ||
return try { | ||
val configFile = getCurrentConfigFile() | ||
var menuItem: MyMenuItem? = null | ||
|
||
if (configFile != null) { | ||
val text = LoadTextUtil.loadText(configFile) | ||
menuItem = Gson().fromJson(text.toString(), MyMenuItem::class.java) | ||
} | ||
|
||
return menuItem | ||
} catch (e: IOException) { | ||
null // TODO: Error Handling | ||
} | ||
} | ||
|
||
fun writeConfigFileContents(newMenu: MyMenuItem, newFileName: String?) { | ||
WriteCommandAction.runWriteCommandAction(project) { | ||
try { | ||
val updatedJSON = Gson().toJson(newMenu, MyMenuItem::class.java) | ||
val configFile = getCurrentConfigFile() | ||
|
||
if (newFileName?.isNotEmpty() == true) { | ||
// Update the plugin's model to remember this new file name | ||
pluginSettingsService.loadState(state = pluginSettingsService.state.copy(fileName = newFileName)) | ||
// Update the file's name with the new value that the user entered, or the existing name | ||
// TODO: Add input validation and verify that this works for changing the file's path (not just renaming) | ||
configFile!!.rename(this, newFileName) | ||
} | ||
// Ensure that the file is writable | ||
configFile!!.isWritable = true | ||
|
||
// Write the new content to the file | ||
// TODO: Check if there is an easy way to have the updated JSON be formatted instead of all on one line | ||
VfsUtil.saveText(configFile, updatedJSON) | ||
} catch (e: IOException) { | ||
// TODO: Error Handling | ||
e.printStackTrace() | ||
} | ||
} | ||
} | ||
|
||
private fun getCurrentConfigFile(): VirtualFile? { | ||
val fileName = pluginSettingsService.state.fileName | ||
return FilenameIndex.getVirtualFilesByName(fileName, GlobalSearchScope.allScope(project)).firstOrNull() | ||
} | ||
|
||
companion object { | ||
fun getInstance(project: Project): FileInputService { | ||
return project.getService(FileInputService::class.java) | ||
} | ||
} | ||
} |
17 changes: 0 additions & 17 deletions
17
src/main/java/org/jetbrains/plugins/template/services/MyProjectService.kt
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.