Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(63)

Unified Diff: server/static/rpcexplorer/rpc-completer.html

Issue 1695893004: RPC Explorer (Closed) Base URL: https://chromium.googlesource.com/external/github.com/luci/luci-go@rpcepxlorer-deps
Patch Set: Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: server/static/rpcexplorer/rpc-completer.html
diff --git a/server/static/rpcexplorer/rpc-completer.html b/server/static/rpcexplorer/rpc-completer.html
new file mode 100644
index 0000000000000000000000000000000000000000..f886d8bcdfd3eb6b963941a8fd1b2c3858758d4c
--- /dev/null
+++ b/server/static/rpcexplorer/rpc-completer.html
@@ -0,0 +1,273 @@
+<!--
+ ~ // Copyright 2016 The Chromium Authors. All rights reserved.
+ ~ // Use of this source code is governed by a BSD-style license that can be
+ ~ // found in the LICENSE file.
+ ~
+ -->
+
+<link rel="import" href="/static/common/third_party/polymer/polymer.html">
+
+<link rel="import" href="rpc-descriptor-util.html">
+
+<!-- The `rpc-completer` element implements Ace editor completer interface
Bons 2016/02/13 17:18:25 newline after <!-- ?
nodir 2016/02/17 02:02:13 Done.
+based on a protobuf message descriptors.
+-->
+<script>
+'use strict';
+
+Polymer({
+ is: 'rpc-completer',
+
+ properties: {
+ description: Object, // FileDescriptorSet message
+ rootTypeName: String,
+ rootType: {
+ type: Object,
+ computed: '_resolveType(description, rootTypeName)'
+ }
+ },
+
+ _resolveType: function(desc, name) {
+ return rpcExplorer.descUtil.resolve(desc, name);
+ },
+
+ skipWhitespace: function(text, i) {
+ var space = {
+ ' ': 1,
+ '\n': 1,
+ '\r': 1,
+ '\t': 1
+ };
+ while (space[text.charAt(i)]) {
+ i++;
+ }
+ return i;
+ },
+
+ findMatching: function(text, i, open, close) {
+ var level = 1;
+ for (; i < text.length; i++) {
+ switch (text.charAt(i)) {
+ case open:
+ level++;
+ break;
+ case close:
+ level--;
+ if (level == 0) {
+ return i;
+ }
+ break;
+ }
+ }
+ return -1;
+ },
+
+ // resolves path at the end of text, best effort.
+ // e.g. for text '{ "a": { "b": [' returns ['a', 'b']
+ // For '{ "a": {}, "b": {' returns ['b'].
+ // For '{ "a":' returns ['a'].
+ getCurrentPath: function(text) {
+ var path = [];
+ for (var i = 0; i < text.length;) {
+ // find colon.
Bons 2016/02/13 17:18:25 this comment is too short and doesn't really tell
nodir 2016/02/17 02:02:13 removed
+ i = text.indexOf(':', i);
+ if (i == -1) {
+ break;
+ }
+ var colon = i;
+
+ i++;
+ i = this.skipWhitespace(text, i);
+
+ if (i == text.length || text.charAt(i) == '"' && i + 1 == text.length) {
+ // the path is a field.
+ } else {
+ // is there an array or object after the colon?
+ var closingIndex = null;
+ switch (text.charAt(i)) {
+ case '{':
+ closingIndex = this.findMatching(text, i + 1, '{', '}');
+ break;
+ case '[':
+ closingIndex = this.findMatching(text, i + 1, '[', ']');
+ break;
+ }
+ if (closingIndex == null || closingIndex != -1) {
+ // Not an object/array or closed. Ignore.
+ continue;
+ }
+ }
+
+ // read the name to the left of colon.
+ var secondQuote = text.lastIndexOf('"', colon);
+ if (secondQuote == -1) {
+ return null;
+ }
+
+ var firstQuote = text.lastIndexOf('"', secondQuote - 1);
+ if (firstQuote == -1) {
+ return null;
+ }
+
+ path.push(text.substring(firstQuote + 1, secondQuote));
+ }
+ return path;
+ },
+
+ // returns elements to display in autocomplete.
+ getCompletions: function(editor, session, pos, prefix, callback) {
+ if (!this.rootType) {
+ return;
+ }
+
+ // Get all text left to the current selection.
+ var beforePos = {
+ start: {row: 0, col: 0 },
+ end: session.selection.getRange().start
+ };
+ var text = session.getTextRange(beforePos);
+ // Resolve path.
+ var path = this.getCurrentPath(text);
+ console.log('completion: current path: ', path);
+
+ // Resolve type.
+ var type = this.rootType;
+ var util = rpcExplorer.descUtil;
+ for (var i = 0; i < path.length; i++) {
+ if (type.type != 'message') {
+ return;
+ }
+ var field = util.findByName(type.desc.field, path[i]);
+ if (!field) {
+ console.log('Field ' + path[i] + ' not found')
+ return;
+ }
+ var typeName = field.type_name;
+ if (typeof typeName != 'string') {
+ console.log('Field ' + path[i] + ' is not a message or enum')
+ return;
+ }
+ // referenced types are often prefixed with '.'
+ typeName = util.trimPrefixDot(typeName);
+ type = util.resolve(this.description, typeName);
+ if (!type) {
+ return;
+ }
+ }
+
+ // Automatically surround with quotes.
+ var quoteCount = (text.match(/"/g) || []).length;
+ var shouldQuote = quoteCount % 2 == 0;
+
+ var completions = [];
+ switch (type.type) {
+ case 'message':
+ if (!type.desc.field) {
+ return;
+ }
+ for (var i = 0; i < type.desc.field.length; i++) {
+ var field = type.desc.field[i];
+ var meta = this.fieldTypeName(field);
+ if (field.label == 'LABEL_REPEATED') {
+ meta = 'repeated ' + meta;
+ }
+ completions.push({
+ desc: field,
+ caption: field.name,
+ snippet: this.snippetForField(field, shouldQuote),
+ meta: meta
+ });
+ }
+ break;
+
+ case 'enum':
+ for (var i = 0; i < type.desc.value.length; i++) {
+ var value = type.desc.value[i];
+ var snippet = value.name;
+ if (shouldQuote) {
+ snippet = '"' + snippet + '"';
+ }
+ completions.push({
+ desc: value,
+ caption: value.name,
+ snippet: snippet,
+ meta: '' + value.number
+ })
+ }
+ break;
+ }
+
+ callback(null, completions);
+ },
+
+ snippetForField: function (field, shouldQuote) {
+ // snippet docs:
+ // https://cloud9-sdk.readme.io/docs/snippets
+ var snippet = field.name;
+ if (shouldQuote) {
+ snippet = '"' + snippet + '"';
+ }
+ if (!shouldQuote) {
+ return snippet;
+ }
+
+ snippet += ': ';
+
+ var open = '';
+ var close = '';
+ if (field.label == 'LABEL_REPEATED') {
+ open += '[';
+ close = ']' + close;
+ }
+
+ switch (field.type) {
+ case 'TYPE_MESSAGE':
+ open += '{';
+ close = '}' + close;
+ break;
+ case 'TYPE_STRING':
+ case 'TYPE_ENUM':
+ open += '"';
+ close = '"' + close;
+ break;
+ }
+
+ // ${0} is the position of cursor after insertion.
+ snippet += open + '${0}' + close;
+ return snippet;
+ },
+
+ // Returns leading comments of a completion.
+ // The result is displayed to the right of the selected completion.
+ getDocTooltip: function(completion) {
+ return completion.desc.source_code_info && completion.desc.source_code_info.leading_comments;
Bons 2016/02/13 17:18:25 80 chars
nodir 2016/02/17 02:02:12 Done.
+ },
+
+ scalarTypeNames: {
+ TYPE_DOUBLE: 'double',
+ TYPE_FLOAT: 'float',
+ TYPE_INT64: 'int64',
+ TYPE_UINT64: 'uint64',
+ TYPE_INT32: 'int32',
+ TYPE_FIXED64: 'fixed64',
+ TYPE_FIXED32: 'fixed32',
+ TYPE_BOOL: 'bool',
+ TYPE_STRING: 'string',
+ TYPE_BYTES: 'bytes',
+ TYPE_UINT32: 'uint32',
+ TYPE_SFIXED32: 'sfixed32',
+ TYPE_SFIXED64: 'sfixed64',
+ TYPE_SINT32: 'sint32',
+ TYPE_SINT64: 'sint64',
+ },
+
+ fieldTypeName: function(field) {
+ var name = this.scalarTypeNames[field.type];
+ if (!name) {
+ name = rpcExplorer.descUtil.trimPrefixDot(field.type_name);
+ }
+ return name;
+ }
+
+})
Bons 2016/02/13 17:18:25 semicolon
nodir 2016/02/17 02:02:13 Done.
+</script>

Powered by Google App Engine
This is Rietveld 408576698