From 0755c30d589d0e5f94093d32753a511ba29839b5 Mon Sep 17 00:00:00 2001 From: Erik Tierney Date: Thu, 25 Apr 2013 10:27:25 -0400 Subject: [PATCH 1/6] Basic support for code hinting in html files. For HTML files, we replace the HTML sections with whitespace, leaving only the text of the script tags for Tern to see. This way the offset/cursor positions don't need to be translated, which would be necessary to get jump-to-def to work. --- .../JavaScriptCodeHints/ScopeManager.js | 12 ++- .../default/JavaScriptCodeHints/Session.js | 93 ++++++++++++++++++- .../default/JavaScriptCodeHints/main.js | 23 ++++- 3 files changed, 119 insertions(+), 9 deletions(-) diff --git a/src/extensions/default/JavaScriptCodeHints/ScopeManager.js b/src/extensions/default/JavaScriptCodeHints/ScopeManager.js index f99fb5b2a0f..1ed86b21ffb 100644 --- a/src/extensions/default/JavaScriptCodeHints/ScopeManager.js +++ b/src/extensions/default/JavaScriptCodeHints/ScopeManager.js @@ -286,7 +286,7 @@ define(function (require, exports, module) { * @return {jQuery.Promise} - The promise will not complete until the tern * hints have completed. */ - function requestHints(session, document, offset) { + function requestHints(session, document) { var path = document.file.fullPath, split = HintUtils.splitPath(path), dir = split.dir, @@ -295,12 +295,14 @@ define(function (require, exports, module) { var $deferredHints = $.Deferred(), hintPromise, fnTypePromise, - propsPromise; + propsPromise, + text = session.getJavascriptText(), + offset = session.getOffset(); - hintPromise = getTernHints(dir, file, offset, document.getText()); + hintPromise = getTernHints(dir, file, offset, text); var sessionType = session.getType(); if (sessionType.property) { - propsPromise = getTernProperties(dir, file, offset, document.getText()); + propsPromise = getTernProperties(dir, file, offset, text); } else { var $propsDeferred = $.Deferred(); propsPromise = $propsDeferred.promise(); @@ -309,7 +311,7 @@ define(function (require, exports, module) { if (sessionType.showFunctionType) { // Show function sig - fnTypePromise = getTernFunctionType(dir, file, sessionType.functionCallPos, offset, document.getText()); + fnTypePromise = getTernFunctionType(dir, file, sessionType.functionCallPos, offset, text); } else { var $fnTypeDeferred = $.Deferred(); fnTypePromise = $fnTypeDeferred.promise(); diff --git a/src/extensions/default/JavaScriptCodeHints/Session.js b/src/extensions/default/JavaScriptCodeHints/Session.js index 2df37b16d4a..fa2e1f8edbc 100644 --- a/src/extensions/default/JavaScriptCodeHints/Session.js +++ b/src/extensions/default/JavaScriptCodeHints/Session.js @@ -21,13 +21,16 @@ * */ -/*jslint vars: true, plusplus: true, devel: true, nomen: true, indent: 4, maxerr: 50 */ +/*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 StringMatch = brackets.getModule("utils/StringMatch"), + LanguageManager = brackets.getModule("language/LanguageManager"), + HTMLUtils = brackets.getModule("language/HTMLUtils"), + TokenUtils = brackets.getModule("utils/TokenUtils"), HintUtils = require("HintUtils"), ScopeManager = require("ScopeManager"); @@ -251,6 +254,16 @@ define(function (require, exports, module) { * function being called */ Session.prototype.getType = function () { + function getLexicalInfo(token) { + if (token.state.lexical) { + // in a javascript file this is just in the state field + return token.state.lexical.info; + } else if (token.state.localState && token.state.localState.lexical) { + // inline javascript in an html file will have this in + // the localState field + return token.state.localState.lexical.info; + } + } var propertyLookup = false, inFunctionCall = false, showFunctionType = false, @@ -262,7 +275,7 @@ define(function (require, exports, module) { if (token) { // if this token is part of a function call, then the tokens lexical info // will be annotated with "call" - if (token.state.lexical.info === "call") { + if (getLexicalInfo(token) === "call") { inFunctionCall = true; if (this.getQuery().length > 0) { inFunctionCall = false; @@ -451,6 +464,82 @@ define(function (require, exports, module) { } return hints; }; + + /** + * Get the javascript text of the file open in the editor for this Session. + * For a javascript file, this is just the text of the file. For an HTML file, + * this will be only the text in the + + some stuff + + + +
+
+ + + some other stuff +
+
+ + + + + \ No newline at end of file From 5d7dc76dfb63722ccbb6163f5916882d3442829c Mon Sep 17 00:00:00 2001 From: Erik Tierney Date: Fri, 3 May 2013 11:29:43 -0400 Subject: [PATCH 4/6] refactor HTMLUtils.findStyleBlocks to find arbitrary blocks. The js code hinting code needed to find script blocks, so refactor the style block code to find any kind of block, and call that instead of having another copy of the code. findStyleBlocks now just calls the generic method, looking for "css" blocks. Also fixed up the block finding code to handle empty blocks, which I ran into when looking for "javascript" blocks. --- .../default/JavaScriptCodeHints/Session.js | 35 +----------- src/language/HTMLUtils.js | 57 ++++++++++++------- 2 files changed, 39 insertions(+), 53 deletions(-) diff --git a/src/extensions/default/JavaScriptCodeHints/Session.js b/src/extensions/default/JavaScriptCodeHints/Session.js index 2081e085d12..475425e32f4 100644 --- a/src/extensions/default/JavaScriptCodeHints/Session.js +++ b/src/extensions/default/JavaScriptCodeHints/Session.js @@ -481,41 +481,10 @@ define(function (require, exports, module) { //