Skip to content

Highlight current function #36

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
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 60 additions & 3 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,39 @@ define(function (require, exports, module) {
SVG: require("src/languages/XML")
};

var editor_lines = 0;

var onKeyActivity = function ($event, editor, event) {
selectCurrentFunction(editor);
}

var onCursorActivity = function ($event, editor) {
selectCurrentFunction(editor);
}

function selectCurrentFunction(editor) {
var pos = editor.getCursorPos();

if (editor_lines != editor.getLastVisibleLine())
{
console.log('changed');
createList();
}
editor_lines = editor.getLastVisibleLine();

var remove_entries = $("#outline-list ul li");
remove_entries.removeClass('active');

var entry = $("#outline-list ul li").filter(function() {
var id = $(this)[0].id;
var splitted = id.split('_');
var line_start = splitted[splitted.length-2];
var line_eind = splitted[splitted.length-1];
return line_start <= pos.line && line_eind >= pos.line;
});
entry.addClass('active');
}

function getOutline() {
var $outline = Mustache.render(ListTemplate, {
Strings: Strings
Expand All @@ -57,6 +90,18 @@ define(function (require, exports, module) {
}

function updateOutline() {

var currentEditor = EditorManager.getActiveEditor();
$(currentEditor).on('keyup', onKeyActivity);
$(currentEditor).on('cursorActivity', onCursorActivity);
$(currentEditor).on('cursorActivity', onCursorActivity);

showOutline();

createList();
}

function createList() {
var doc = DocumentManager.getCurrentDocument();
if (!doc) {
hideOutline();
Expand All @@ -69,25 +114,36 @@ define(function (require, exports, module) {
return;
}

showOutline();

var lines = doc.getText(false).split("\n");
var list = lang.getOutlineList(lines, prefs.get("args"), prefs.get("unnamed"));

if (prefs.get("sort") && lang.compare) {
list.sort(lang.compare);
}

list.reverse();

var last_line = 999999999;
list.forEach(function (entry) {
entry.end_line = last_line;
last_line = entry.line - 1;
});

list.reverse();

$("#outline-list ul li").remove();
list.forEach(function (entry) {
var $entry = $(document.createElement("li"));
$entry.addClass("outline-entry");
$entry.addClass(entry.classes);
$entry.append(entry.$html);
$entry[0].id = 'outline_line_' + entry.line + '_' + entry.end_line;
$entry.click({
line: entry.line,
ch: entry.ch
}, goToLine);
$("#outline-list ul").append($entry);
last_line = entry.line;
});
}

Expand Down Expand Up @@ -141,6 +197,7 @@ define(function (require, exports, module) {
$("#outline-toolbar-icon").addClass("enabled");
EditorManager.on("activeEditorChange", updateOutline);
DocumentManager.on("documentSaved", updateOutline);

updateOutline();
}

Expand Down Expand Up @@ -186,4 +243,4 @@ define(function (require, exports, module) {
if (prefs.get("enabled")) {
enableOutline();
}
});
});
5 changes: 5 additions & 0 deletions styles/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
background-position: 0 -24px;
}

.outline-entry.active {
background: #0F1114;
font-weight: 700;
}


/* ==== Outline List ==== */

Expand Down