Skip to content
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

VIM-1341 implement gx command #773

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion annotation-processors/build.gradle.kts
Expand Up @@ -23,7 +23,7 @@ repositories {
dependencies {
compileOnly("com.google.devtools.ksp:symbol-processing-api:1.9.21-1.0.15")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json-jvm:$kotlinxSerializationVersion") {
// kotlin stdlib is provided by IJ, so there is no need to include it into the distribution
// kotlin stdlib is provided by IJ, so there is no need to include it into the
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Accidental change?

exclude("org.jetbrains.kotlin", "kotlin-stdlib")
exclude("org.jetbrains.kotlin", "kotlin-stdlib-common")
}
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/maddyhome/idea/vim/package-info.java
Expand Up @@ -428,6 +428,7 @@
* |gu| {@link com.maddyhome.idea.vim.action.change.change.ChangeCaseLowerMotionAction}
* |gv| {@link com.maddyhome.idea.vim.action.motion.visual.VisualSelectPreviousAction}
* |gw| TO BE IMPLEMENTED
* |gx| {@link com.maddyhome.idea.vim.action.motion.search.GotoUrlAction}
* |g@| {@link com.maddyhome.idea.vim.action.change.OperatorAction}
* |g~| {@link com.maddyhome.idea.vim.action.change.change.ChangeCaseToggleMotionAction}
* |g<Down>| TO BE IMPLEMENTED
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/META-INF/includes/VimActions.xml
Expand Up @@ -315,6 +315,7 @@
<vimAction implementation="com.maddyhome.idea.vim.action.motion.search.SearchWordForwardAction" mappingModes="NXO" keys="g*"/>
<vimAction implementation="com.maddyhome.idea.vim.action.motion.search.SearchWordBackwardAction" mappingModes="NXO" keys="g#"/>
<vimAction implementation="com.maddyhome.idea.vim.action.motion.search.GotoDeclarationAction" mappingModes="NX" keys="gD,gd,«C-]»"/>
<vimAction implementation="com.maddyhome.idea.vim.action.motion.search.GotoUrlAction" mappingModes="N" keys="gx"/>

<!-- Macro -->
<vimAction implementation="com.maddyhome.idea.vim.action.macro.ToggleRecordingAction" mappingModes="NX" keys="q"/>
Expand Down
@@ -0,0 +1,73 @@
/*
* Copyright 2003-2023 The IdeaVim authors
*
* Use of this source code is governed by an MIT-style
* license that can be found in the LICENSE.txt file or at
* https://opensource.org/licenses/MIT.
*/
package com.maddyhome.idea.vim.action.motion.search

import com.intellij.vim.annotations.CommandOrMotion
import com.intellij.vim.annotations.Mode
import com.maddyhome.idea.vim.api.ExecutionContext
import com.maddyhome.idea.vim.api.VimEditor
import com.maddyhome.idea.vim.api.injector
import com.maddyhome.idea.vim.command.Command
import com.maddyhome.idea.vim.command.CommandFlags
import com.maddyhome.idea.vim.command.OperatorArguments
import com.maddyhome.idea.vim.diagnostic.vimLogger
import com.maddyhome.idea.vim.handler.VimActionHandler
import com.maddyhome.idea.vim.helper.enumSetOf
import com.maddyhome.idea.vim.helper.vimStateMachine
import com.maddyhome.idea.vim.macro.VimMacroBase.Companion.logger
import java.util.*
import java.util.concurrent.Future
import java.util.regex.Pattern

@CommandOrMotion(keys = ["gx"], modes = [Mode.NORMAL, Mode.VISUAL])
public class GotoUrlAction : VimActionHandler.SingleExecution() {
override val type: Command.Type = Command.Type.OTHER_READONLY
private val URL_REGEX = "^((https?|ftp)://|(www|ftp)\\.)?[a-z0-9-]+(\\.[a-z0-9-]+)+([/?].*)?$"
private val pattern = Pattern.compile(URL_REGEX);

override fun execute(
editor: VimEditor,
context: ExecutionContext,
cmd: Command,
operatorArguments: OperatorArguments,
): Boolean {
val wordUnderCursor = exactWordUnderCursor(editor);
logger.info("word: $wordUnderCursor")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Logging is always great, but we try not to log the raw text from user input. The problem is that this text may have sensitive information like passwords and we don't want to leak it into the log.
I see several cases here, it's better to clean them up.

if(!isValidUrl(wordUnderCursor)){
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm thinking about the logic behind links search.
Generally, it looks valid to me, but IJ also has a feature to navigate to issues in Jira or youtrack by issue id. Like // Here is my issue: VIM-123. This, of course, won't be supported by this logic.
I actually found a fancy util in the platform: IssueNavigationConfiguration.getInstance(project).findIssueLinks(commentText). You can feed it a bunch of text and it will return resolved links (Including transformation from VIM-123 to https://youtrack.com/issues/VIM-123).
I think, it will be better to use this util. This, however, will make the code platform dependent, and we'll have two options here: move this GotoUrlAction out from vim-engine or make an abstraction via VimInjector. The second option would require to add a new function to VimInjector like VimApplication.resolveLinks and implement it via the mentioned util in IjVimInjector.

logger.info("word $wordUnderCursor in not url")
return false;
}
injector.jumpService.saveJumpLocation(editor)
injector.actionExecutor.executeAction("GotoDeclaration", context)
return true
}

private fun exactWordUnderCursor(editor: VimEditor): String {
var col = editor.currentCaret().vimLastColumn;
var line = editor.getLineText(editor.currentCaret().vimLine - 1);
if(line.isBlank() || line.get(col).isWhitespace()){
return "";
}
logger.info("col: $col line: $line")
var start = col;
var end = col;
while( start > 0 && !line.get(start).isWhitespace()){
start--;
}
while(end < line.length && !line.get(end).isWhitespace()){
end++;
}
logger.info("start: $start end $end")
return line.substring(start+1, end);
}

private fun isValidUrl(url: String): Boolean {
var matcher = pattern.matcher(url)
return matcher.matches()
}
}