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

Side by Side Diff: third_party/WebKit/Source/devtools/front_end/sdk/RuntimeModel.js

Issue 2468493004: DevTools: Consolidate completion code into JavaScriptAutocomplete.js (Closed)
Patch Set: merge Created 4 years, 1 month 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 unified diff | Download patch
« no previous file with comments | « third_party/WebKit/Source/devtools/front_end/console/ConsolePrompt.js ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2012 Google Inc. All rights reserved. 2 * Copyright (C) 2012 Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are 5 * modification, are permitted provided that the following conditions are
6 * met: 6 * met:
7 * 7 *
8 * * Redistributions of source code must retain the above copyright 8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above 10 * * Redistributions in binary form must reproduce the above
(...skipping 583 matching lines...) Expand 10 before | Expand all | Expand 10 after
594 console.error(error); 594 console.error(error);
595 callback(null); 595 callback(null);
596 return; 596 return;
597 } 597 }
598 callback(this.runtimeModel.createRemoteObject(result), exceptionDeta ils); 598 callback(this.runtimeModel.createRemoteObject(result), exceptionDeta ils);
599 } 599 }
600 this.target().runtimeAgent().evaluate(expression, objectGroup, includeCo mmandLineAPI, silent, this.id, returnByValue, generatePreview, userGesture, fals e, evalCallback.bind(this)); 600 this.target().runtimeAgent().evaluate(expression, objectGroup, includeCo mmandLineAPI, silent, this.id, returnByValue, generatePreview, userGesture, fals e, evalCallback.bind(this));
601 }, 601 },
602 602
603 /** 603 /**
604 * @param {string} expressionString
605 * @param {string} prefix
606 * @param {boolean=} force
607 * @return {!Promise<!Array<string>>}
608 */
609 completionsForExpression: function(expressionString, prefix, force)
610 {
611 var lastIndex = expressionString.length - 1;
612
613 var dotNotation = (expressionString[lastIndex] === ".");
614 var bracketNotation = (expressionString[lastIndex] === "[");
615
616 if (dotNotation || bracketNotation)
617 expressionString = expressionString.substr(0, lastIndex);
618
619 // User is entering float value, do not suggest anything.
620 if (expressionString && !isNaN(expressionString))
621 return Promise.resolve([]);
622
623 if (!prefix && !expressionString && !force)
624 return Promise.resolve([]);
625
626 var fufill;
627 var promise = new Promise(x => fufill = x);
628 if (!expressionString && this.debuggerModel.selectedCallFrame())
629 this.debuggerModel.selectedCallFrame().variableNames(receivedPropert yNames.bind(this));
630 else
631 this.evaluate(expressionString, "completion", true, true, false, fal se, false, evaluated.bind(this));
632
633 return promise;
634 /**
635 * @param {?WebInspector.RemoteObject} result
636 * @param {!RuntimeAgent.ExceptionDetails=} exceptionDetails
637 * @this {WebInspector.ExecutionContext}
638 */
639 function evaluated(result, exceptionDetails)
640 {
641 if (!result || !!exceptionDetails) {
642 fufill([]);
643 return;
644 }
645
646 /**
647 * @param {?WebInspector.RemoteObject} object
648 * @return {!Promise<?WebInspector.RemoteObject>}
649 */
650 function extractTarget(object)
651 {
652 if (!object)
653 return Promise.resolve(/** @type {?WebInspector.RemoteObject } */(null));
654 if (object.type !== "object" || object.subtype !== "proxy")
655 return Promise.resolve(/** @type {?WebInspector.RemoteObject } */(object));
656 return object.getOwnPropertiesPromise().then(extractTargetFromPr operties).then(extractTarget);
657 }
658
659 /**
660 * @param {!{properties: ?Array<!WebInspector.RemoteObjectProperty>, internalProperties: ?Array<!WebInspector.RemoteObjectProperty>}} properties
661 * @return {?WebInspector.RemoteObject}
662 */
663 function extractTargetFromProperties(properties)
664 {
665 var internalProperties = properties.internalProperties || [];
666 var target = internalProperties.find(property => property.name = == "[[Target]]");
667 return target ? target.value : null;
668 }
669
670 /**
671 * @param {string=} type
672 * @return {!Object}
673 * @suppressReceiverCheck
674 * @this {Object}
675 */
676 function getCompletions(type)
677 {
678 var object;
679 if (type === "string")
680 object = new String("");
681 else if (type === "number")
682 object = new Number(0);
683 else if (type === "boolean")
684 object = new Boolean(false);
685 else
686 object = this;
687
688 var resultSet = { __proto__: null };
689 try {
690 for (var o = object; o; o = Object.getPrototypeOf(o)) {
691 if ((type === "array" || type === "typedarray") && o === object && ArrayBuffer.isView(o) && o.length > 9999)
692 continue;
693 var names = Object.getOwnPropertyNames(o);
694 var isArray = Array.isArray(o);
695 for (var i = 0; i < names.length; ++i) {
696 // Skip array elements indexes.
697 if (isArray && /^[0-9]/.test(names[i]))
698 continue;
699 resultSet[names[i]] = true;
700 }
701 }
702 } catch (e) {
703 }
704 return resultSet;
705 }
706
707 /**
708 * @param {?WebInspector.RemoteObject} object
709 * @this {WebInspector.ExecutionContext}
710 */
711 function completionsForObject(object)
712 {
713 if (!object)
714 receivedPropertyNames.call(this, null);
715 else if (object.type === "object" || object.type === "function")
716 object.callFunctionJSON(getCompletions, [WebInspector.Remote Object.toCallArgument(object.subtype)], receivedPropertyNames.bind(this));
717 else if (object.type === "string" || object.type === "number" || object.type === "boolean")
718 this.evaluate("(" + getCompletions + ")(\"" + result.type + "\")", "completion", false, true, true, false, false, receivedPropertyNamesFromE val.bind(this));
719 }
720
721 extractTarget(result).then(completionsForObject.bind(this));
722 }
723
724 /**
725 * @param {?WebInspector.RemoteObject} result
726 * @param {!RuntimeAgent.ExceptionDetails=} exceptionDetails
727 * @this {WebInspector.ExecutionContext}
728 */
729 function receivedPropertyNamesFromEval(result, exceptionDetails)
730 {
731 this.target().runtimeAgent().releaseObjectGroup("completion");
732 if (result && !exceptionDetails)
733 receivedPropertyNames.call(this, /** @type {!Object} */(result.v alue));
734 else
735 fufill([]);
736 }
737
738 /**
739 * @param {?Object} propertyNames
740 * @this {WebInspector.ExecutionContext}
741 */
742 function receivedPropertyNames(propertyNames)
743 {
744 this.target().runtimeAgent().releaseObjectGroup("completion");
745 if (!propertyNames) {
746 fufill([]);
747 return;
748 }
749 var includeCommandLineAPI = (!dotNotation && !bracketNotation);
750 if (includeCommandLineAPI) {
751 const commandLineAPI = ["dir", "dirxml", "keys", "values", "prof ile", "profileEnd", "monitorEvents", "unmonitorEvents", "inspect", "copy", "clea r",
752 "getEventListeners", "debug", "undebug", "monitor", "unmonit or", "table", "$", "$$", "$x"];
753 for (var i = 0; i < commandLineAPI.length; ++i)
754 propertyNames[commandLineAPI[i]] = true;
755 }
756 fufill(this._completionsForPrefix(dotNotation, bracketNotation, expr essionString, prefix, Object.keys(propertyNames)));
757 }
758 },
759
760 /**
761 * @param {boolean} dotNotation
762 * @param {boolean} bracketNotation
763 * @param {string} expressionString
764 * @param {string} prefix
765 * @param {!Array.<string>} properties
766 * @return {!Array<string>}
767 */
768 _completionsForPrefix: function(dotNotation, bracketNotation, expressionStri ng, prefix, properties) {
769 if (bracketNotation) {
770 if (prefix.length && prefix[0] === "'")
771 var quoteUsed = "'";
772 else
773 var quoteUsed = "\"";
774 }
775
776 var results = [];
777
778 if (!expressionString) {
779 const keywords = ["break", "case", "catch", "continue", "default", " delete", "do", "else", "finally", "for", "function", "if", "in",
780 "instanceof", "new", "return", "switch", "this", " throw", "try", "typeof", "var", "void", "while", "with"];
781 properties = properties.concat(keywords);
782 }
783
784 properties.sort();
785
786 for (var i = 0; i < properties.length; ++i) {
787 var property = properties[i];
788
789 // Assume that all non-ASCII characters are letters and thus can be used as part of identifier.
790 if (dotNotation && !/^[a-zA-Z_$\u008F-\uFFFF][a-zA-Z0-9_$\u008F-\uFF FF]*$/.test(property))
791 continue;
792
793 if (bracketNotation) {
794 if (!/^[0-9]+$/.test(property))
795 property = quoteUsed + property.escapeCharacters(quoteUsed + "\\") + quoteUsed;
796 property += "]";
797 }
798
799 if (property.length < prefix.length)
800 continue;
801 if (prefix.length && !property.startsWith(prefix))
802 continue;
803
804 // Substitute actual newlines with newline characters. @see crbug.co m/498421
805 results.push(property.split("\n").join("\\n"));
806 }
807 return results;
808 },
809
810 /**
811 * @return {string} 604 * @return {string}
812 */ 605 */
813 label: function() 606 label: function()
814 { 607 {
815 return this._label; 608 return this._label;
816 }, 609 },
817 610
818 /** 611 /**
819 * @param {string} label 612 * @param {string} label
820 */ 613 */
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after
1013 /** 806 /**
1014 * @return {boolean} 807 * @return {boolean}
1015 */ 808 */
1016 isNormalListenerType: function() 809 isNormalListenerType: function()
1017 { 810 {
1018 return this._listenerType === "normal"; 811 return this._listenerType === "normal";
1019 }, 812 },
1020 813
1021 __proto__: WebInspector.SDKObject.prototype 814 __proto__: WebInspector.SDKObject.prototype
1022 }; 815 };
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/devtools/front_end/console/ConsolePrompt.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698