Skip to content

Conversation

@zuberol
Copy link
Contributor

@zuberol zuberol commented Jun 27, 2025

…cription

val home = System.getProperty("user.home")
if (home != null) {
return home + path.substring(1)
expanded = home + substring(1)
Copy link
Member

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.

Copy link
Member

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_]+")
Copy link
Member

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()
Copy link
Member

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.

@AlexPl292
Copy link
Member

Also, in the commit message please always add a reference to the commit you're working on.

@AlexPl292 AlexPl292 requested a review from Copilot July 2, 2025 07:58
Copy link

Copilot AI left a 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 expandUser helper with a new vimExpanded string extension
  • Implements ~ and $VAR expansions directly in vimExpanded
  • 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 vimExpanded logic; 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("~")) {
Copy link

Copilot AI Jul 2, 2025

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 'username'. This can produce invalid paths; consider restricting to '/' or the exact '' case before expansion.

Suggested change
if (startsWith("~")) {
if (this == "~" || startsWith("~/")) {

Copilot uses AI. Check for mistakes.
}
return path

val envRe = Regex("\\$[A-Za-z0-9_]+")
Copy link

Copilot AI Jul 2, 2025

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.

Suggested change
val envRe = Regex("\\$[A-Za-z0-9_]+")

Copilot uses AI. Check for mistakes.
@AlexPl292
Copy link
Member

Yay, copilot now supports Kotlin 🔥

@zuberol zuberol changed the title adds expansion to ":source" command, following ":help expand-env" VIM-2143 expand environment variables in source command Jul 17, 2025
@zuberol zuberol changed the title VIM-2143 expand environment variables in source command Feat(VIM-2143) expand environment variables in source command Jul 17, 2025
@AlexPl292
Copy link
Member

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.

@AlexPl292 AlexPl292 closed this Oct 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants