This repository has been archived by the owner on Sep 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
inlinedocs.js
128 lines (108 loc) · 4.21 KB
/
inlinedocs.js
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
/*jslint vars: true, plusplus: true, devel: true, nomen: true, indent: 4, maxerr: 50, regexp: true, */
/*global define, brackets, $ */
define(function (require, exports, module) {
'use strict';
var PreferencesManager = brackets.getModule("preferences/PreferencesManager");
var StatusBar = brackets.getModule("widgets/StatusBar");
var DocumentManager = brackets.getModule("document/DocumentManager");
var prefs = PreferencesManager.getExtensionPrefs("robotframework");
var robot = require("./robot");
var InlineDocsViewer = require("InlineDocsViewer");
function callRFHub(url) {
$.ajaxSetup({ "async": false});
var result = null;
var response = $.getJSON(url, function (data) {
StatusBar.updateIndicator("rfhub-error", false);
result = data;
}).fail(function (jqxhr, textStatus, error) {
StatusBar.updateIndicator("rfhub-error", true);
});
$.ajaxSetup({ "async": true});
return result;
}
function findKeywordDocs(editor, pos) {
// try to find keyword docs, starting at the current cell
// and working backwards until we find something.
var cm = editor._codeMirror;
var cells = robot.getCellContents(cm, pos.line);
var n = robot.getCurrentCellNumber(cm, pos);
var docs=null;
var kw;
while (n > 0 && docs === null) {
kw = cells[n].replace(/^ +| +$/g,'');
docs = getKeywordDocs(editor, kw);
n -= 1;
}
return docs;
}
function getKeywordDocs(editor, kw) {
// Get documentation for the given keyword by querying the hub
var prefs = PreferencesManager.getExtensionPrefs("robotframework");
var hub_url = prefs.get("hub-url");
var url = hub_url + "/api/keywords?pattern=^" + kw + "$";
var result = null;
var data = callRFHub(url);
if (data !== null && data.keywords.length > 0) {
result = {
args: data.keywords[0].args,
doc: data.keywords[0].doc,
htmldoc: data.keywords[0].htmldoc,
library: data.keywords[0].library,
library_url: data.keywords[0].library_url,
name: data.keywords[0].name
};
return result;
}
return null;
}
function inlineDocsProvider(editor, pos) {
var langId,
sel,
kw,
docs,
tmp,
inlineWidget,
result;
// Only provide docs when cursor is in robot file
langId = editor.getLanguageForSelection().getId();
if (langId !== "robot") {
return null;
}
// Only provide docs if there is no multiline selection
sel = editor.getSelection();
if (sel.start.line !== sel.end.line) {
return null;
}
var docs = findKeywordDocs(editor, pos);
if (docs === null) {
return false;
}
if (docs.args) {
// the arguments come back as a json array; deserialize
// them and convert to a human-readable list
try {
docs.args = JSON.parse(docs.args).join(", ");
if (docs.args == "") {
docs.args = "<i>none</i>"
}
} catch (e) {
// do nothing. We'll accept our fate and display
// the arguments in their raw format.
}
} else {
docs.args = "<i>none</i>"
}
inlineWidget = new InlineDocsViewer(docs.name, {
keyword_name: docs.name,
keyword_doc: docs.htmldoc,
keyword_args: docs.args,
keyword_library: docs.library,
rfhub_url: "http://localhost:7070/doc/keywords/BuiltIn/Repeat%20Keyword"
});
result = new $.Deferred();
inlineWidget.load(editor);
result.resolve(inlineWidget);
return result.promise();
}
exports.inlineDocsProvider = inlineDocsProvider;
});