- 
                Notifications
    
You must be signed in to change notification settings  - Fork 799
 
Feat(VIM-2143) expand environment variables in source command #1222
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
Conversation
| val home = System.getProperty("user.home") | ||
| if (home != null) { | ||
| return home + path.substring(1) | ||
| expanded = home + substring(1) | 
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.
Hi, TBH I find a floating substrint(1) extremely confusing. I didn't even realize what the receiver of this function.
Please write an explicit receiver like this.substring.
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.
Same for startsWith("~")
| } | ||
| return path | ||
| 
               | 
          ||
| val envRe = Regex("\\$[A-Za-z0-9_]+") | 
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.
I feel like this approach skips some cases. For example, the env variable can have brackets ${PATH}, or the dollar can be escaped like \$PATH.
On windows, the variables use a different syntax %PATH%.
Also, I'm not sure if the env variable can start with the number.
This expansion seems to be a non-trivial task. I'd try to search some existing ways in java/kotlin to expand it. Or, maybe there is an existing utility in the IJ platform (you can ask about it in #ij-dev slack channel).
If nothing will work, let's try to write our own expander, but keeping in mind the cases I described. Probably, it would make sense to read the specification on the env variables (if there is any), or ask AI what cases there are.
| return path | ||
| 
               | 
          ||
| val envRe = Regex("\\$[A-Za-z0-9_]+") | ||
| val env = System.getenv() | 
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.
As I see from the help you referencing, this expansion may be also useful in other places. Can I ask you to move it to the injector, to one of the classes?
Also, since this functionality is not trivial, it'll require some tests.
| 
           Also, in the commit message please always add a reference to the commit you're working on.  | 
    
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.
Pull Request Overview
Enhance the :source command by adding user-home and environment-variable expansion to file paths.
- Replaces the old 
expandUserhelper with a newvimExpandedstring extension - Implements 
~and$VARexpansions directly invimExpanded - Updates command invocation to use 
argument.trim().vimExpanded() 
Comments suppressed due to low confidence (1)
vim-engine/src/main/kotlin/com/maddyhome/idea/vim/vimscript/model/commands/SourceCommand.kt:51
- There are no unit tests covering the new 
vimExpandedlogic; add tests for both tilde and environment-variable expansion to ensure correct behavior. 
  private fun String.vimExpanded(): String {
| if (path.startsWith("~")) { | ||
| private fun String.vimExpanded(): String { | ||
| var expanded = this | ||
| if (startsWith("~")) { | 
    
      
    
      Copilot
AI
    
    
    
      Jul 2, 2025 
    
  
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.
The tilde expansion applies to any string beginning with '', including '' case before expansion.username'. This can produce invalid paths; consider restricting to '/' or the exact '
| if (startsWith("~")) { | |
| if (this == "~" || startsWith("~/")) { | 
| } | ||
| return path | ||
| 
               | 
          ||
| val envRe = Regex("\\$[A-Za-z0-9_]+") | 
    
      
    
      Copilot
AI
    
    
    
      Jul 2, 2025 
    
  
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.
[nitpick] Compiling the regex on each call can incur overhead; consider moving envRe to a companion object or top-level constant so it's compiled once.
| val envRe = Regex("\\$[A-Za-z0-9_]+") | 
| 
           Yay, copilot now supports Kotlin 🔥  | 
    
| 
           Hi, I’m closing this PR due to inactivity. If you’d like to resume work on it, you’re welcome to reopen it anytime.  | 
    
…cription