Skip to content

Commit

Permalink
Open valid url under cursor
Browse files Browse the repository at this point in the history
  • Loading branch information
anijanyan committed Oct 19, 2022
1 parent 5294aa2 commit d6e15f5
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/commands/default_commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,10 @@ exports.commands = [{
multiSelectAction: "forEach",
scrollIntoView: "cursor",
readOnly: true
}, {
name: "jumptodef",
bindKey: bindKey("Ctrl+F3", "F3"),
exec: function(editor) { editor.jumptodef(); }
}, {
name: "joinlines",
description: "Join lines",
Expand Down
32 changes: 32 additions & 0 deletions src/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -1729,6 +1729,38 @@ Editor.$uid = 0;
}
};

/**
* Finds link at defined {row} and {column}
* @returns {String}
**/
this.findLinkAt = function (row,column) {
var line = this.session.getLine(row);
var wordParts = line.split(/((?:https?|ftp):\/\/[\S]+)/);
var delta = column;
if (delta < 0) delta = 0;
var curLength = 0, itLength = 0, match;
wordParts.forEach(function (item) {
itLength = curLength + item.length;
if (delta >= curLength && delta <= itLength) {
if (item.match(/((?:https?|ftp):\/\/[\S]+)/)) {
match = item.replace(/[\s:.,'";}\]]+$/, "");
}
}
curLength = itLength;
});
return match;
};

/**
* Open valid url under cursor in another tab
**/
this.jumptodef = function () {
var cursor = this.selection.getCursor();
var url = this.findLinkAt(cursor.row, cursor.column);
if (url)
window.open(url, '_blank');
};

/**
* Removes all the lines in the current selection
* @related EditSession.remove
Expand Down

0 comments on commit d6e15f5

Please sign in to comment.