-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
170 lines (138 loc) · 4.72 KB
/
Copy pathmain.js
File metadata and controls
170 lines (138 loc) · 4.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/**
Brackets Markdown Extension
---------
This is awesome
- One
- Two
- More
Some Code:
def function
with(code)
end
Blabla
_italics_ and __bold__
*/
/*jslint vars: true, plusplus: true, devel: true, browser: true, nomen: true, indent: 4, maxerr: 50 */
/*global define, $ */
define(function (require, exports, module) {
'use strict';
var MarkdownInlineDocumentation = require("MarkdownInlineDocumentation"),
InlineMarkdownViewer = require("InlineMarkdownViewer");
var DocumentManager = brackets.libRequire('document/DocumentManager'),
EditorManager = brackets.libRequire('editor/EditorManager');
/**
* Method to test if a line is a comment
* @param {Number} line
* @return {boolean} True if the line is a comment
*/
function _isCommentLine(hostEditor, line) {
var eol = hostEditor._codeMirror.getLine(line).length;
var token = hostEditor._codeMirror.getTokenAt({line:line, ch: eol});
return (token.className == "comment");
}
/**
* This function is registered with EditManager as an inline editor provider. It creates a
* MarkdownInlineDocumentationEditor when cursor is on an comment written in markdown.
* This is then shown in the editor
*
* @param {!Editor} editor
* @param {!{line:Number, ch:Number}} pos
* @return {$.Promise} a promise that will be resolved with an InlineWidget
* or null if we're not going to provide anything.
*/
function markdownProvider(hostEditor, pos) {
// Only provide a markdown editor when cursor is in JS content
if (hostEditor._codeMirror.getOption("mode") !== "javascript") {
return null;
}
// Only provide markdown editor when cursor is in comment
if (! _isCommentLine(hostEditor, pos.line)) {
return null;
}
//search for start of comment block
var start = pos.line;
do
{
start--;
}
while ((start > 0) && (_isCommentLine(hostEditor, start)));
//search for end of comment block
var end = pos.line;
do
{
end++;
}
while ((end < hostEditor.lineCount()) && (_isCommentLine(hostEditor, end)));
var result = $.Deferred();
var inlineEditor = new MarkdownInlineDocumentation(start+1, end-1);
inlineEditor.load(hostEditor);
result.resolve (inlineEditor);
return result.promise();
}
/**
* This function iterates through all comment blocks in the given document
* and calls the callback for each comment block.
* @param hostEditor
* @param callback Gets 3 arguments: The editor, the start, and the end line of the block.
*/
function _eachCommentInEditor (hostEditor, callback) {
var start = -1;
for (var i = 0; i < hostEditor.lineCount(); i++) {
var text = hostEditor.getLineText(i);
if (start < 0) {
// not inside a comment -> look for /**
if (/^\s*\/\*\*/.test(text)) {
start = i;
}
}
else {
// inside a comment -> look for */
if (/\*\//.test(text)) {
callback(hostEditor, start, i);
start = -1;
}
}
}
}
function _onCurrentDocumentChange () {
var currentDocument = DocumentManager.getCurrentDocument();
if (!currentDocument) {
return;
}
var currentHostEditor = EditorManager.getCurrentFullEditor();
// currentHostEditor._codeMirror.operation(function (){
_eachCommentInEditor(currentHostEditor, function eachCommentCallback (hostEditor, start, end) {
var inlineMarkdownViewer = new InlineMarkdownViewer();
inlineMarkdownViewer.load(hostEditor, start, end);
hostEditor.addInlineWidget({line: (start > 0) ? start -1 : end + 1, ch:0}, inlineMarkdownViewer);
});
// });
}
function _loadStyles() {
var request = new XMLHttpRequest();
request.open("GET", "extensions/user/markdown/main.less", true);
request.onload = function onLoad(event) {
var parser = new less.Parser();
parser.parse(request.responseText, function onParse(err, tree) {
console.assert(!err, err);
var style = $("<style>" + tree.toCSS() + "</style>");
$(document.head).append(style);
// now all resources for succesful rendering are loaded
init();
});
};
request.send(null);
}
/**
* This function is run when the css is loaded
*/
function init() {
$(DocumentManager).on('currentDocumentChange', _onCurrentDocumentChange);
_onCurrentDocumentChange();
}
console.log("loading MarkdownInlineDocumentation!");
// load our stylesheet
_loadStyles();
// init
// $(init);
});