I'm writing an Intellij Plugin. I want to retrieve a file from the project and add a new function to the class using Kotlin poet .
IntelliJ IDEA Plugin - How to modify the contents of an existing file using Kotlin poet
254 views Asked by Anup Ammanavar At
1
There are 1 answers
Related Questions in INTELLIJ-PLUGIN
- IntelliJ plugin development - loop over only git tracked files
- Git hooks in IntelliJ
- camunda admin not responding
- How to write Java source code as a String to file in IntelliJ plugin
- Java Swing ignores attributes set to the specific components
- New Karate Plugin update is giving issue in intelliJ
- How can I modify Kotlin code from a self-developed Intellij plugin?
- KtFile cannot be cast to KtFile: Problem with Kotlin PSI while plugin development
- How to not block ui running plugin in intellij idea
- Maven build - Given path to java executor does not exist "C:\somepath\jdk\zulujdk-11\bin\java"
- Build ZIP with Gradle and upload it for every commit using GitHub Actions
- How to access PSI structure for both Java, Python in a single Intellij Plugin
- IntelliJ does not serialize nested objects' properties of PersistentStateComponent subclass
- Pass arguments through CLI while building plugin for intellij. I am using gradle
- How to Handle (Ghost) Duplicate Files and Use Drag Move in Intellij Language Resource Bundle?
Related Questions in KOTLINPOET
- Using KotlinPoet, is it possible to add a `@field:SomeAnnotation`?
- How to prevent importing class defined in the same file?
- Kotlin Poet ambigouos imports
- kotlinpoet generate code to call builder class continuously
- IntelliJ IDEA Plugin - How to modify the contents of an existing file using Kotlin poet
- Modify Kotlin compiled code output based on annotation (not generate separate code)
- Android Studio redeclaration error when compiling release while having Kotlin Poet generated classes
- How to pass arguments to super using KotlinPoet
- How to add a function definition available in a string to a class generated from kotlinpoet TypeSpec?
- How to get KClass<List<String>> type from List<String> in kotlin
- adding star projection in kotlin poet
- Kotlin Annotation processor doesn't add import for generated files
- KotlinPoet: how to generate a DSL with parameters
- how to programatically mark generated source folder as Generated Sources Root
- How to implement the suspend keyword to modify higher-order functions in kotlin poet
Popular Questions
- How do I undo the most recent local commits in Git?
- How can I remove a specific item from an array in JavaScript?
- How do I delete a Git branch locally and remotely?
- Find all files containing a specific text (string) on Linux?
- How do I revert a Git repository to a previous commit?
- How do I create an HTML button that acts like a link?
- How do I check out a remote Git branch?
- How do I force "git pull" to overwrite local files?
- How do I list all files of a directory?
- How to check whether a string contains a substring in JavaScript?
- How do I redirect to another webpage?
- How can I iterate over rows in a Pandas DataFrame?
- How do I convert a String to an int in Java?
- Does Python have a string 'contains' substring method?
- How do I check if a string contains a specific word?
Popular Tags
Trending Questions
- UIImageView Frame Doesn't Reflect Constraints
- Is it possible to use adb commands to click on a view by finding its ID?
- How to create a new web character symbol recognizable by html/javascript?
- Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
- Heap Gives Page Fault
- Connect ffmpeg to Visual Studio 2008
- Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
- How to avoid default initialization of objects in std::vector?
- second argument of the command line arguments in a format other than char** argv or char* argv[]
- How to improve efficiency of algorithm which generates next lexicographic permutation?
- Navigating to the another actvity app getting crash in android
- How to read the particular message format in android and store in sqlite database?
- Resetting inventory status after order is cancelled
- Efficiently compute powers of X in SSE/AVX
- Insert into an external database using ajax and php : POST 500 (Internal Server Error)
First of, you will need an anchor
PsiElementin your psi file. This can probably be a method that comes before the method you want to insert. Finding an anchor element consistently can be tricky and depends on your context. As I'm not familiar with the psi structure of Kotlin files, I'll leave this up to you. I'll assume that you have an element calledpsiElement.Then you have to create your new element. You can create a
PsiFileusing thePsiFileFactory, and then extract the psi element you need. I often like to create a helper class with utility methods that can extract the right kind of psi elements for me, I imagine for your case it could similar to the following. (Once again, I'm not familiar with the Kotlin psi structure, so this is just a gist to give you an idea.)You can then use this to create a new element and add it after
psiElement.Note that there are multiple ways to insert
newElementwith respect topsiElement, it will depend on the structure of your file what works best.Feel free to have a look at
LatexPsiHelperto see an example of such a helper class and how it's used.