|
| 1 | +/* |
| 2 | + * Copyright (c) 2017 - present Adobe Systems Incorporated. All rights reserved. |
| 3 | + * |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining a |
| 5 | + * copy of this software and associated documentation files (the "Software"), |
| 6 | + * to deal in the Software without restriction, including without limitation |
| 7 | + * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 | + * and/or sell copies of the Software, and to permit persons to whom the |
| 9 | + * Software is furnished to do so, subject to the following conditions: |
| 10 | + * |
| 11 | + * The above copyright notice and this permission notice shall be included in |
| 12 | + * all copies or substantial portions of the Software. |
| 13 | + * |
| 14 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 19 | + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 20 | + * DEALINGS IN THE SOFTWARE. |
| 21 | + * |
| 22 | + */ |
| 23 | + |
| 24 | +define(function (require, exports, module) { |
| 25 | + "use strict"; |
| 26 | + |
| 27 | + // Load dependent modules |
| 28 | + var AppInit = brackets.getModule("utils/AppInit"), |
| 29 | + CodeHintManager = brackets.getModule("editor/CodeHintManager"), |
| 30 | + AtRulesText = require("text!AtRulesDef.json"), |
| 31 | + AtRules = JSON.parse(AtRulesText); |
| 32 | + |
| 33 | + |
| 34 | + /** |
| 35 | + * @constructor |
| 36 | + */ |
| 37 | + function AtRuleHints() { |
| 38 | + } |
| 39 | + |
| 40 | + // As we are only going to provide @rules name hints |
| 41 | + // we should claim that we don't have hints for anything else |
| 42 | + AtRuleHints.prototype.hasHints = function (editor, implicitChar) { |
| 43 | + var pos = editor.getCursorPos(), |
| 44 | + token = editor._codeMirror.getTokenAt(pos), |
| 45 | + cmState; |
| 46 | + |
| 47 | + this.editor = editor; |
| 48 | + |
| 49 | + if (token.state.base && token.state.base.localState) { |
| 50 | + cmState = token.state.base.localState; |
| 51 | + } else { |
| 52 | + cmState = token.state; |
| 53 | + } |
| 54 | + |
| 55 | + // Check if we are at '@' rule 'def' context |
| 56 | + if ((token.type === "def" && cmState.context.type === "at") |
| 57 | + || (token.type === "variable-2" && (cmState.context.type === "top" || cmState.context.type === "block"))) { |
| 58 | + this.filter = token.string; |
| 59 | + return true; |
| 60 | + } else { |
| 61 | + this.filter = null; |
| 62 | + return false; |
| 63 | + } |
| 64 | + }; |
| 65 | + |
| 66 | + AtRuleHints.prototype.getHints = function (implicitChar) { |
| 67 | + var pos = this.editor.getCursorPos(), |
| 68 | + token = this.editor._codeMirror.getTokenAt(pos); |
| 69 | + |
| 70 | + this.filter = token.string; |
| 71 | + this.token = token; |
| 72 | + |
| 73 | + if (!this.filter) { |
| 74 | + return null; |
| 75 | + } |
| 76 | + |
| 77 | + // Filter the property list based on the token string |
| 78 | + var result = Object.keys(AtRules).filter(function (key) { |
| 79 | + if (key.indexOf(token.string) === 0) { |
| 80 | + return key; |
| 81 | + } |
| 82 | + }).sort(); |
| 83 | + |
| 84 | + return { |
| 85 | + hints: result, |
| 86 | + match: this.filter, |
| 87 | + selectInitial: true, |
| 88 | + defaultDescriptionWidth: true, |
| 89 | + handleWideResults: false |
| 90 | + }; |
| 91 | + }; |
| 92 | + |
| 93 | + |
| 94 | + /** |
| 95 | + * Inserts a given @<rule> hint into the current editor context. |
| 96 | + * |
| 97 | + * @param {string} completion |
| 98 | + * The hint to be inserted into the editor context. |
| 99 | + * |
| 100 | + * @return {boolean} |
| 101 | + * Indicates whether the manager should follow hint insertion with an |
| 102 | + * additional explicit hint request. |
| 103 | + */ |
| 104 | + AtRuleHints.prototype.insertHint = function (completion) { |
| 105 | + var cursor = this.editor.getCursorPos(); |
| 106 | + this.editor.document.replaceRange(completion, {line: cursor.line, ch: this.token.start}, {line: cursor.line, ch: this.token.end}); |
| 107 | + return false; |
| 108 | + }; |
| 109 | + |
| 110 | + AppInit.appReady(function () { |
| 111 | + // Register code hint providers |
| 112 | + var restrictedBlockHints = new AtRuleHints(); |
| 113 | + CodeHintManager.registerHintProvider(restrictedBlockHints, ["css", "less", "scss"], 0); |
| 114 | + |
| 115 | + // For unit testing |
| 116 | + exports.restrictedBlockHints = restrictedBlockHints; |
| 117 | + }); |
| 118 | +}); |
0 commit comments