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

Open valid url under cursor #4970

Merged
merged 3 commits into from
Oct 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: "openlink",
bindKey: bindKey("Ctrl+F3", "F3"),
exec: function(editor) { editor.openLink(); }
}, {
name: "joinlines",
description: "Join lines",
Expand Down
35 changes: 35 additions & 0 deletions src/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -1729,6 +1729,41 @@ 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 columnPosition = column;
if (columnPosition < 0) columnPosition = 0;
var previousPosition = 0, currentPosition = 0, match;
for (let item of wordParts) {
currentPosition = previousPosition + item.length;
if (columnPosition >= previousPosition && columnPosition <= currentPosition) {
if (item.match(/((?:https?|ftp):\/\/[\S]+)/)) {
match = item.replace(/[\s:.,'";}\]]+$/, "");
Copy link
Contributor

Choose a reason for hiding this comment

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

should we add break here so we don't look at the rest of the line? it can help with large lines

break;
}
}
previousPosition = currentPosition;
}
return match;
};

/**
* Open valid url under cursor in another tab
* @returns {Boolean}
**/
this.openLink = function () {
var cursor = this.selection.getCursor();
var url = this.findLinkAt(cursor.row, cursor.column);
if (url)
window.open(url, '_blank');
return url != null;
};

/**
* Removes all the lines in the current selection
* @related EditSession.remove
Expand Down
12 changes: 10 additions & 2 deletions src/editor_commands_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ if (typeof process !== "undefined") {

"use strict";

var ace = require("./ace");
var EditSession = require("./edit_session").EditSession;
var Editor = require("./editor").Editor;
var UndoManager = require("./undomanager").UndoManager;
var MockRenderer = require("./test/mockrenderer").MockRenderer;
Expand Down Expand Up @@ -533,6 +531,16 @@ module.exports = {
exec("selectlineend", 1);
editor.execCommand(editor.commands.byName.joinlines);
assert.equal(editor.getValue(), "foo for foo foo foo for foo foo\nfoo for foo foo");
},
"test findlink": function() {
editor = new Editor(new MockRenderer());

editor.setValue("foo for foo foo\nhttps://www.google.com/", 1);
var url = editor.findLinkAt(0, 1);
assert.equal(url, null);

url = editor.findLinkAt(1, 5);
assert.equal(url, "https://www.google.com/");
}
};

Expand Down