Skip to content

Commit

Permalink
initial commit: implement gx command
Browse files Browse the repository at this point in the history
  • Loading branch information
Bryan Tidwell committed Dec 4, 2023
1 parent 9d5aa83 commit 73b85a6
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 1 deletion.
2 changes: 1 addition & 1 deletion annotation-processors/build.gradle.kts
Original file line number Diff line number Diff line change
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
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
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")
if(!isValidUrl(wordUnderCursor)){
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()
}
}

0 comments on commit 73b85a6

Please sign in to comment.