-
Notifications
You must be signed in to change notification settings - Fork 752
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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") | ||
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. 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. |
||
if(!isValidUrl(wordUnderCursor)){ | ||
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. I'm thinking about the logic behind links search. |
||
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() | ||
} | ||
} |
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.
Accidental change?