token_tooltip.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /* ***** BEGIN LICENSE BLOCK *****
  2. * Distributed under the BSD license:
  3. *
  4. * Copyright (c) 2010, Ajax.org B.V.
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * * Neither the name of Ajax.org B.V. nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL AJAX.ORG B.V. BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * ***** END LICENSE BLOCK ***** */
  30. define(function(require, exports, module) {
  31. "use strict";
  32. var dom = require("ace/lib/dom");
  33. var oop = require("ace/lib/oop");
  34. var event = require("ace/lib/event");
  35. var Range = require("ace/range").Range;
  36. var Tooltip = require("ace/tooltip").Tooltip;
  37. function TokenTooltip (editor) {
  38. if (editor.tokenTooltip)
  39. return;
  40. Tooltip.call(this, editor.container);
  41. editor.tokenTooltip = this;
  42. this.editor = editor;
  43. this.update = this.update.bind(this);
  44. this.onMouseMove = this.onMouseMove.bind(this);
  45. this.onMouseOut = this.onMouseOut.bind(this);
  46. event.addListener(editor.renderer.scroller, "mousemove", this.onMouseMove);
  47. event.addListener(editor.renderer.content, "mouseout", this.onMouseOut);
  48. }
  49. oop.inherits(TokenTooltip, Tooltip);
  50. (function(){
  51. this.token = {};
  52. this.range = new Range();
  53. this.update = function() {
  54. this.$timer = null;
  55. var r = this.editor.renderer;
  56. if (this.lastT - (r.timeStamp || 0) > 1000) {
  57. r.rect = null;
  58. r.timeStamp = this.lastT;
  59. this.maxHeight = window.innerHeight;
  60. this.maxWidth = window.innerWidth;
  61. }
  62. var canvasPos = r.rect || (r.rect = r.scroller.getBoundingClientRect());
  63. var offset = (this.x + r.scrollLeft - canvasPos.left - r.$padding) / r.characterWidth;
  64. var row = Math.floor((this.y + r.scrollTop - canvasPos.top) / r.lineHeight);
  65. var col = Math.round(offset);
  66. var screenPos = {row: row, column: col, side: offset - col > 0 ? 1 : -1};
  67. var session = this.editor.session;
  68. var docPos = session.screenToDocumentPosition(screenPos.row, screenPos.column);
  69. var token = session.getTokenAt(docPos.row, docPos.column);
  70. if (!token && !session.getLine(docPos.row)) {
  71. token = {
  72. type: "",
  73. value: "",
  74. state: session.bgTokenizer.getState(0)
  75. };
  76. }
  77. if (!token) {
  78. session.removeMarker(this.marker);
  79. this.hide();
  80. return;
  81. }
  82. var tokenText = token.type;
  83. if (token.state)
  84. tokenText += "|" + token.state;
  85. if (token.merge)
  86. tokenText += "\n merge";
  87. if (token.stateTransitions)
  88. tokenText += "\n " + token.stateTransitions.join("\n ");
  89. if (this.tokenText != tokenText) {
  90. this.setText(tokenText);
  91. this.width = this.getWidth();
  92. this.height = this.getHeight();
  93. this.tokenText = tokenText;
  94. }
  95. this.show(null, this.x, this.y);
  96. this.token = token;
  97. session.removeMarker(this.marker);
  98. this.range = new Range(docPos.row, token.start, docPos.row, token.start + token.value.length);
  99. this.marker = session.addMarker(this.range, "ace_bracket", "text");
  100. };
  101. this.onMouseMove = function(e) {
  102. this.x = e.clientX;
  103. this.y = e.clientY;
  104. if (this.isOpen) {
  105. this.lastT = e.timeStamp;
  106. this.setPosition(this.x, this.y);
  107. }
  108. if (!this.$timer)
  109. this.$timer = setTimeout(this.update, 100);
  110. };
  111. this.onMouseOut = function(e) {
  112. if (e && e.currentTarget.contains(e.relatedTarget))
  113. return;
  114. this.hide();
  115. this.editor.session.removeMarker(this.marker);
  116. this.$timer = clearTimeout(this.$timer);
  117. };
  118. this.setPosition = function(x, y) {
  119. if (x + 10 + this.width > this.maxWidth)
  120. x = window.innerWidth - this.width - 10;
  121. if (y > window.innerHeight * 0.75 || y + 20 + this.height > this.maxHeight)
  122. y = y - this.height - 30;
  123. Tooltip.prototype.setPosition.call(this, x + 10, y + 20);
  124. };
  125. this.destroy = function() {
  126. this.onMouseOut();
  127. event.removeListener(this.editor.renderer.scroller, "mousemove", this.onMouseMove);
  128. event.removeListener(this.editor.renderer.content, "mouseout", this.onMouseOut);
  129. delete this.editor.tokenTooltip;
  130. };
  131. }).call(TokenTooltip.prototype);
  132. exports.TokenTooltip = TokenTooltip;
  133. });