-
Notifications
You must be signed in to change notification settings - Fork 1
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
fix: Use VirtualFiles to save dot file, improved logging #115
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,22 +4,18 @@ import com.intellij.ide.plugins.PluginManagerCore | |
import com.intellij.notification.Notification | ||
import com.intellij.notification.NotificationType | ||
import com.intellij.notification.Notifications | ||
import com.intellij.openapi.application.ApplicationManager | ||
import com.intellij.openapi.application.runInEdt | ||
import com.intellij.openapi.command.WriteCommandAction | ||
import com.intellij.openapi.application.runWriteAction | ||
import com.intellij.openapi.diagnostic.Logger | ||
import com.intellij.openapi.extensions.PluginId | ||
import com.intellij.openapi.fileTypes.FileTypeManager | ||
import com.intellij.openapi.project.Project | ||
import com.intellij.openapi.vfs.VfsUtil | ||
import com.intellij.psi.PsiDirectory | ||
import com.intellij.psi.PsiFileFactory | ||
import com.intellij.psi.PsiManager | ||
import com.intellij.openapi.project.guessProjectDir | ||
import com.intellij.openapi.vfs.* | ||
import com.vaadin.plugin.copilot.handler.* | ||
import com.vaadin.plugin.utils.VaadinIcons | ||
import io.netty.handler.codec.http.HttpResponseStatus | ||
import java.io.BufferedWriter | ||
import java.io.File | ||
import java.io.IOException | ||
import java.io.StringWriter | ||
import java.util.* | ||
|
||
|
@@ -95,53 +91,61 @@ class CopilotPluginUtil { | |
} | ||
} | ||
props.store(bufferedWriter, "Vaadin Copilot Integration Runtime Properties") | ||
|
||
val fileType = FileTypeManager.getInstance().getStdFileType("properties") | ||
runInEdt { | ||
ApplicationManager.getApplication().runWriteAction { | ||
dotFileDirectory.findFile(DOTFILE)?.delete() | ||
val file = | ||
PsiFileFactory.getInstance(project) | ||
.createFileFromText(DOTFILE, fileType, stringWriter.toString()) | ||
dotFileDirectory.add(file) | ||
LOG.info("$DOTFILE created in ${dotFileDirectory.virtualFile.path}") | ||
runWriteAction { | ||
val dotFile = dotFileDirectory.findFile(DOTFILE) | ||
dotFile?.let { | ||
try { | ||
it.delete(this) | ||
} catch (e: IOException) { | ||
LOG.error("Failed to delete $DOTFILE: ${e.message}") | ||
} | ||
} | ||
val newFile = | ||
try { | ||
dotFileDirectory.createChildData(this, DOTFILE) | ||
} catch (e: IOException) { | ||
LOG.error("Failed to create $DOTFILE: ${e.message}") | ||
return@runWriteAction | ||
} | ||
|
||
try { | ||
VfsUtil.saveText(newFile, stringWriter.toString()) | ||
} catch (e: IOException) { | ||
LOG.error("Failed to write to $DOTFILE: ${e.message}") | ||
} | ||
LOG.info("$newFile created") | ||
} | ||
} | ||
} else { | ||
LOG.error("Cannot create $DOTFILE") | ||
} | ||
} | ||
|
||
fun removeDotFile(project: Project) { | ||
ApplicationManager.getApplication().runWriteAction { | ||
val dotFileDirectory = getDotFileDirectory(project) | ||
dotFileDirectory?.findFile(DOTFILE)?.let { | ||
it.delete() | ||
LOG.info("$DOTFILE removed from ${dotFileDirectory.virtualFile.path}") | ||
return@runWriteAction | ||
} | ||
LOG.warn("Cannot remove $DOTFILE") | ||
} | ||
} | ||
|
||
private fun getIdeaDir(project: Project): File { | ||
return File(project.basePath, IDEA_DIR) | ||
} | ||
|
||
fun getDotFileDirectory(project: Project): PsiDirectory? { | ||
return ApplicationManager.getApplication().runReadAction<PsiDirectory?> { | ||
VfsUtil.findFileByIoFile(getIdeaDir(project), false)?.let { | ||
return@runReadAction PsiManager.getInstance(project).findDirectory(it) | ||
runInEdt { | ||
runWriteAction { | ||
val dotFile = getDotFileDirectory(project)?.findFile(DOTFILE) | ||
dotFile?.let { | ||
try { | ||
it.delete(this) | ||
LOG.info("$it removed") | ||
} catch (e: IOException) { | ||
LOG.error("Failed to delete $DOTFILE: ${e.message}") | ||
} | ||
return@runWriteAction | ||
} | ||
LOG.warn("Cannot remove $dotFile - file does not exist") | ||
} | ||
return@runReadAction null | ||
} | ||
} | ||
|
||
fun createIdeaDirectoryIfMissing(project: Project) { | ||
WriteCommandAction.runWriteCommandAction(project) { | ||
val ideaDir = getIdeaDir(project).path | ||
VfsUtil.createDirectoryIfMissing(ideaDir)?.let { LOG.info("$ideaDir created") } | ||
private fun getDotFileDirectory(project: Project): VirtualFile? { | ||
val projectDir = project.guessProjectDir() | ||
if (projectDir == null) { | ||
LOG.error("Cannot guess project directory") | ||
return null | ||
} | ||
LOG.info("Project directory: $projectDir") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this info or debug info? |
||
return projectDir.findOrCreateDirectory(IDEA_DIR) | ||
} | ||
} | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this info or debug info?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
According to https://plugins.jetbrains.com/docs/intellij/ide-infrastructure.html#logging
INFO
and above goes toidea.log
by default. As I'm looking into other log files entries ours fits perfectly. It's information about work of plugin so I would keep it asINFO
.