forked from phcode-dev/staging.phcode.dev
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.js
More file actions
1 lines (1 loc) · 6.01 KB
/
main.js
File metadata and controls
1 lines (1 loc) · 6.01 KB
1
define(function(require,exports,module){var AppInit=brackets.getModule("utils/AppInit"),CodeHintManager=brackets.getModule("editor/CodeHintManager"),PreferencesManager=brackets.getModule("preferences/PreferencesManager"),XMLUtils=brackets.getModule("language/XMLUtils"),StringMatch=brackets.getModule("utils/StringMatch"),ColorUtils=brackets.getModule("utils/ColorUtils"),Strings=brackets.getModule("strings"),_=brackets.getModule("thirdparty/lodash"),SVGTags=require("text!SVGTags.json"),SVGAttributes=require("text!SVGAttributes.json"),cachedAttributes={},tagData,attributeData,isSVGEnabled,stringMatcherOptions={preferPrefixMatches:!0};function _isSVGHintsEnabled(){return!1!==PreferencesManager.get("codehint.SVGHints")&&!1!==PreferencesManager.get("showCodeHints")}function getTagAttributes(tagName){var tag;return cachedAttributes.hasOwnProperty(tagName)||(tag=tagData.tags[tagName],cachedAttributes[tagName]=[],tag.attributes&&(cachedAttributes[tagName]=cachedAttributes[tagName].concat(tag.attributes)),tag.attributeGroups.forEach(function(group){tagData.attributeGroups.hasOwnProperty(group)&&(cachedAttributes[tagName]=cachedAttributes[tagName].concat(tagData.attributeGroups[group]))}),cachedAttributes[tagName]=_.uniq(cachedAttributes[tagName].sort(),!0)),cachedAttributes[tagName]}function formatHints(hints,query){var hasColorSwatch=hints.some(function(token){return token.color});return StringMatch.basicMatchSort(hints),hints.map(function(token){var $hintObj=$("<span>").addClass("brackets-svg-hints brackets-hints");return token.stringRanges?token.stringRanges.forEach(function(item){item.matched?$hintObj.append($("<span>").text(item.text).addClass("matched-hint")):$hintObj.append(item.text)}):$hintObj.text(token.value),hasColorSwatch&&($hintObj=ColorUtils.formatColorHint($hintObj,token.color)),$hintObj})}function SVGCodeHints(){this.tagInfo=null}PreferencesManager.definePreference("codehint.SVGHints","boolean",!0,{description:Strings.DESCRIPTION_SVG_HINTS}),PreferencesManager.on("change","codehint.SVGHints",function(){isSVGEnabled=_isSVGHintsEnabled()}),PreferencesManager.on("change","showCodeHints",function(){isSVGEnabled=_isSVGHintsEnabled()}),isSVGEnabled=_isSVGHintsEnabled(),SVGCodeHints.prototype.hasHints=function(editor,implicitChar){return!!(isSVGEnabled&&"image/svg+xml"===editor.getModeForSelection()&&(this.editor=editor,this.tagInfo=XMLUtils.getTagInfo(this.editor,this.editor.getCursorPos()),this.tagInfo&&this.tagInfo.tokenType))},SVGCodeHints.prototype.getHints=function(implicitChar){var hints=[],query,tagInfo,attributes=[],options=[],index,isMultiple,tagSpecificOptions;if(tagInfo=XMLUtils.getTagInfo(this.editor,this.editor.getCursorPos()),this.tagInfo=tagInfo,tagInfo&&tagInfo.tokenType){if(query=tagInfo.token.string.substr(0,tagInfo.offset).trim(),tagInfo.tokenType===XMLUtils.TOKEN_TAG)hints=$.map(Object.keys(tagData.tags),function(tag){var match=StringMatch.stringMatch(tag,query,stringMatcherOptions);if(match)return match});else if(tagInfo.tokenType===XMLUtils.TOKEN_ATTR){if(!tagData.tags[tagInfo.tagName])return null;attributes=getTagAttributes(tagInfo.tagName),hints=$.map(attributes,function(attribute){if(-1===tagInfo.exclusionList.indexOf(attribute)){var match=StringMatch.stringMatch(attribute,query,stringMatcherOptions);if(match)return match}})}else if(tagInfo.tokenType===XMLUtils.TOKEN_VALUE){if(index=tagInfo.tagName+"/"+tagInfo.attrName,tagSpecificOptions=attributeData[index],!tagData.tags[tagInfo.tagName]&&!attributeData[tagInfo.attrName]&&!tagSpecificOptions)return null;if(tagSpecificOptions?(options=tagSpecificOptions.attribOptions,isMultiple=tagSpecificOptions.multiple):attributeData[tagInfo.attrName]&&(options=attributeData[tagInfo.attrName].attribOptions,isMultiple=attributeData[tagInfo.attrName].multiple,"color"===attributeData[tagInfo.attrName].type&&(options=(options=ColorUtils.COLOR_NAMES.map(function(color){return{text:color,color:color}})).concat(["currentColor","transparent"]))),!isMultiple&&/\s+/.test(tagInfo.token.string))return null;query=XMLUtils.getValueQuery(tagInfo),hints=$.map(options,function(option){if(-1===tagInfo.exclusionList.indexOf(option)){var match=StringMatch.stringMatch(option.text||option,query,stringMatcherOptions);if(match)return option.color&&(match.color=option.color),match}})}return{hints:formatHints(hints,query),match:null,selectInitial:!0,handleWideResults:!1}}return null},SVGCodeHints.prototype.insertHint=function(completion){var tagInfo=this.tagInfo,pos=this.editor.getCursorPos(),start={line:-1,ch:-1},end={line:-1,ch:-1},query,startChar,endChar,quoteChar;return completion.jquery&&(completion=completion.text()),start.line=end.line=pos.line,tagInfo.tokenType===XMLUtils.TOKEN_TAG?(start.ch=pos.ch-tagInfo.offset,end.ch=tagInfo.token.end,this.editor.document.replaceRange(completion,start,end),!1):tagInfo.tokenType===XMLUtils.TOKEN_ATTR?tagInfo.shouldReplace?(start.ch=tagInfo.token.start,end.ch=tagInfo.token.end,this.editor.document.replaceRange(completion,start,end),this.editor.setCursorPos(start.line,start.ch+completion.length),!1):(completion+='=""',XMLUtils.regexWhitespace.test(tagInfo.token.string)?start.ch=end.ch=pos.ch:(start.ch=pos.ch-tagInfo.offset,end.ch=pos.ch),this.editor.document.replaceRange(completion,start,end),this.editor.setCursorPos(start.line,start.ch+completion.length-1),!0):tagInfo.tokenType===XMLUtils.TOKEN_VALUE?(startChar=tagInfo.token.string.charAt(0),endChar=tagInfo.token.string.substr(-1,1),quoteChar=/^['"]$/.test(startChar)?startChar:'"',/^['"]$/.test(startChar)||(completion=quoteChar+completion),/^['"]$/.test(endChar)&&1!==tagInfo.token.string.length||(completion+=quoteChar),query=XMLUtils.getValueQuery(tagInfo),start.ch=pos.ch-query.length,end.ch=pos.ch,this.editor.document.replaceRange(completion,start,end),/^['"]$/.test(tagInfo.token.string.substr(tagInfo.offset,1))&&this.editor.setCursorPos(pos.line,start.ch+completion.length+1),!1):void 0},AppInit.appReady(function(){tagData=JSON.parse(SVGTags),attributeData=JSON.parse(SVGAttributes);var hintProvider=new SVGCodeHints;CodeHintManager.registerHintProvider(hintProvider,["svg"],0),exports.hintProvider=hintProvider})});