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) · 10.6 KB
/
main.js
File metadata and controls
1 lines (1 loc) · 10.6 KB
1
define(function(require,exports,module){var AppInit=brackets.getModule("utils/AppInit"),CodeHintManager=brackets.getModule("editor/CodeHintManager"),CSSUtils=brackets.getModule("language/CSSUtils"),FileSystem=brackets.getModule("filesystem/FileSystem"),FileUtils=brackets.getModule("file/FileUtils"),HTMLUtils=brackets.getModule("language/HTMLUtils"),PreferencesManager=brackets.getModule("preferences/PreferencesManager"),ProjectManager=brackets.getModule("project/ProjectManager"),StringUtils=brackets.getModule("utils/StringUtils"),PathUtils=brackets.getModule("thirdparty/path-utils/path-utils"),Strings=brackets.getModule("strings"),Data=require("text!data.json"),urlHints,data,htmlAttrs,styleModes=["css","text/x-less","text/x-scss"];function UrlCodeHints(){}function _clearCachedHints(){urlHints&&urlHints.cachedHints&&urlHints.cachedHints.deferred&&"pending"!==urlHints.cachedHints.deferred.state()&&(urlHints.cachedHints=null)}PreferencesManager.definePreference("codehint.UrlCodeHints","boolean",!0,{description:Strings.DESCRIPTION_URL_CODE_HINTS}),UrlCodeHints.prototype._getUrlList=function(query){var directory,doc,docDir,queryDir="",queryUrl,result=[],self,targetDir,unfiltered=[];return(doc=this.editor&&this.editor.document)&&doc.file?(docDir=FileUtils.getDirectoryPath(doc.file.fullPath),(queryUrl=PathUtils.parseUrl(query.queryStr))&&(queryDir=queryUrl.directory),targetDir=queryDir.length>0&&"/"===queryDir[0]?ProjectManager.getProjectRoot().fullPath+decodeURI(queryDir).substring(1):docDir+decodeURI(queryDir),this.cachedHints&&(this.cachedHints.query&&this.cachedHints.query.tag===query.tag&&this.cachedHints.query.attrName===query.attrName&&this.cachedHints.queryDir===queryDir&&this.cachedHints.docDir===docDir||(this.cachedHints=null)),this.cachedHints?(unfiltered=this.cachedHints.unfiltered,"."===queryUrl.filename?result.push(queryDir+"."):".."===queryUrl.filename&&result.push(queryDir+".."),unfiltered.forEach(function(item){result.push(item)}),result):(directory=FileSystem.getDirectoryForPath(targetDir),(self=this).cachedHints&&self.cachedHints.deferred&&self.cachedHints.deferred.reject(),self.cachedHints={},self.cachedHints.deferred=$.Deferred(),self.cachedHints.unfiltered=[],directory.getContents(function(err,contents){var currentDeferred,entryStr,syncResults;err||(contents.forEach(function(entry){ProjectManager.shouldIndex(entry)&&(entryStr=queryDir+entry._name,entry._isDirectory&&(entryStr+="/"),unfiltered.push(entryStr))}),self.cachedHints.unfiltered=unfiltered,self.cachedHints.query=query,self.cachedHints.queryDir=queryDir,self.cachedHints.docDir=docDir,"rejected"!==self.cachedHints.deferred.state()&&(currentDeferred=self.cachedHints.deferred,(syncResults=self._getUrlList(query))instanceof Array?currentDeferred.resolveWith(self,[syncResults]):(currentDeferred&&"pending"===currentDeferred.state()&¤tDeferred.reject(),self.cachedHints.deferred&&"pending"===self.cachedHints.deferred.state()&&(self.cachedHints.deferred.reject(),self.cachedHints.deferred=null))))}),self.cachedHints.deferred)):result},UrlCodeHints.prototype._getUrlHints=function(query){var hints=[],sortFunc;return-1===query.queryStr.indexOf("?")&&(this.closeOnSelect=!1,hints=this._getUrlList(query),sortFunc=StringUtils.urlSort),{hints:hints,sortFunc:sortFunc}},UrlCodeHints.prototype.hasHints=function(editor,implicitChar){var mode=editor.getModeForSelection();return"html"===mode?this.hasHtmlHints(editor,implicitChar):styleModes.indexOf(mode)>-1&&this.hasCssHints(editor,implicitChar)},UrlCodeHints.prototype.hasCssHints=function(editor,implicitChar){this.editor=editor;var cursor=this.editor.getCursorPos();if(this.info=CSSUtils.getInfoAtPos(editor,cursor),this.info.context!==CSSUtils.PROP_VALUE&&this.info.context!==CSSUtils.IMPORT_URL)return!1;var i,val="";for(i=0;i<=this.info.index&&i<this.info.values.length;i++)i<this.info.index?val+=this.info.values[i]:val+=this.info.values[i].substring(0,this.info.offset);return!!val.match(/^\s*url\(/i)},UrlCodeHints.prototype.hasHtmlHints=function(editor,implicitChar){var tagInfo,query,tokenType;if(this.editor=editor,query=null,(tokenType=(tagInfo=HTMLUtils.getTagInfo(editor,editor.getCursorPos())).position.tokenType)===HTMLUtils.ATTR_VALUE&&htmlAttrs[tagInfo.attr.name]){query=tagInfo.position.offset>=0?tagInfo.attr.value.slice(0,tagInfo.position.offset):"";var hintsAndSortFunc,hints=this._getUrlHints({queryStr:query}).hints;if(hints instanceof Array){var i,foundPrefix=!1;for(query=query.toLowerCase(),i=0;i<hints.length;i++)if(0===hints[i].toLowerCase().indexOf(query)){foundPrefix=!0;break}foundPrefix||(query=null)}}return null!==query},UrlCodeHints.prototype.getHints=function(key){var mode=this.editor.getModeForSelection(),cursor=this.editor.getCursorPos(),filter="",hints=[],sortFunc,query={queryStr:""},result=[];if("html"===mode){var tagInfo=HTMLUtils.getTagInfo(this.editor,cursor),tokenType;if(tagInfo.position.tokenType!==HTMLUtils.ATTR_VALUE||!htmlAttrs[tagInfo.attr.name])return null;tagInfo.position.offset>=0&&(query.queryStr=tagInfo.attr.value.slice(0,tagInfo.position.offset)),this.info=tagInfo}else{if(!(styleModes.indexOf(mode)>-1))return null;this.info=CSSUtils.getInfoAtPos(this.editor,cursor);var context=this.info.context;if(context!==CSSUtils.PROP_VALUE&&context!==CSSUtils.IMPORT_URL)return null;if(-1!==this.info.index){var i,val="";for(i=0;i<this.info.index;i++)val+=this.info.values[i];this.info.index<this.info.values.length&&(val+=this.info.values[this.info.index].substr(0,this.info.offset));var matchWhitespace=(val=val.replace(/^\s*url\(/i,"")).match(/^\s*/);matchWhitespace?(this.info.leadingWhitespace=matchWhitespace[0],val=val.substring(matchWhitespace[0].length)):this.info.leadingWhitespace=null,val.match(/^["']/)?(this.info.openingQuote=val[0],val=val.substring(1)):this.info.openingQuote=null,query.queryStr=val}}if(null!==query.queryStr){filter=query.queryStr;var hintsAndSortFunc=this._getUrlHints(query);hints=hintsAndSortFunc.hints,sortFunc=hintsAndSortFunc.sortFunc}if(this.info.filter=filter,hints instanceof Array&&hints.length){var lowerCaseFilter=filter.toLowerCase();return console.assert(!result.length),{hints:result=$.map(hints,function(item){if(0===item.toLowerCase().indexOf(lowerCaseFilter))return item}).sort(sortFunc),match:query.queryStr,selectInitial:!0,handleWideResults:!1}}if(hints instanceof Object&&hints.hasOwnProperty("done")){var deferred=$.Deferred();return hints.done(function(asyncHints){var lowerCaseFilter=filter.toLowerCase();result=$.map(asyncHints,function(item){if(0===item.toLowerCase().indexOf(lowerCaseFilter))return item}).sort(sortFunc),deferred.resolveWith(this,[{hints:result,match:query.queryStr,selectInitial:!0,handleWideResults:!1}])}),deferred}return null},UrlCodeHints.prototype.insertHint=function(completion){var mode=this.editor.getModeForSelection();return completion=encodeURI(completion),"html"===mode?this.insertHtmlHint(completion):styleModes.indexOf(mode)>-1&&this.insertCssHint(completion)},UrlCodeHints.prototype.getCharOffset=function(array,pos1,pos2){var i,count=0;if(pos1.index===pos2.index)return pos2.offset>=pos1.offset?pos2.offset-pos1.offset:0;if(pos1.index<pos2.index){if(pos1.index<0||pos1.index>=array.length||pos2.index<0||pos2.index>=array.length)return 0;for(i=pos1.index;i<=pos2.index;i++)i===pos1.index?count+=array[i].length-pos1.offset:i===pos2.index?count+=pos2.offset:count+=array[i].length}return count},UrlCodeHints.prototype.findNextPosInArray=function(array,ch,pos){var i,o,searchOffset;for(i=pos.index;i<array.length;i++)if(searchOffset=i===pos.index?pos.offset:0,-1!==(o=array[i].indexOf(ch,searchOffset)))return{index:i,offset:o};return{index:-1,offset:-1}},UrlCodeHints.prototype.insertCssHint=function(completion){var cursor=this.editor.getCursorPos(),start={line:cursor.line,ch:cursor.ch},end={line:cursor.line,ch:cursor.ch},hasClosingQuote=!1,hasClosingParen=!1,insertText=completion,moveLen=0,closingPos={index:-1,offset:-1},searchResult={index:-1,offset:-1};if(this.info.context!==CSSUtils.PROP_VALUE&&this.info.context!==CSSUtils.IMPORT_URL)return!1;if(this.closeOnSelect||null!==completion.match(/\/$/)||(this.closeOnSelect=!0),this.info.openingQuote&&(hasClosingQuote=-1!==(closingPos=this.findNextPosInArray(this.info.values,this.info.openingQuote,this.info)).index),hasClosingParen=hasClosingQuote?-1!==(searchResult=this.findNextPosInArray(this.info.values,")",closingPos)).index:-1!==(closingPos=this.findNextPosInArray(this.info.values,")",this.info)).index,this.closeOnSelect)-1!==closingPos.index&&(end.ch+=this.getCharOffset(this.info.values,this.info,closingPos));else{var nextSlash=this.findNextPosInArray(this.info.values,"/",this.info);nextSlash.index===this.info.index&&nextSlash.offset===this.info.offset&&(end.ch+=1)}return this.info.filter.length>0&&(start.ch-=this.info.filter.length),this.info.openingQuote&&!hasClosingQuote&&(insertText+=this.info.openingQuote),hasClosingParen||(this.info.leadingWhitespace&&(insertText+=this.info.leadingWhitespace),insertText+=")"),this.editor._codeMirror.replaceRange(insertText,start,end),this.closeOnSelect?((moveLen=(hasClosingQuote?1:0)+(hasClosingParen?1:0))>0&&this.editor.setCursorPos(start.line,start.ch+completion.length+moveLen),!1):((moveLen=(this.info.openingQuote&&!hasClosingQuote?1:0)+(hasClosingParen?0:1))>0&&this.editor.setCursorPos(start.line,start.ch+completion.length),!0)},UrlCodeHints.prototype.insertHtmlHint=function(completion){var cursor=this.editor.getCursorPos(),start={line:-1,ch:-1},end={line:-1,ch:-1},tagInfo=HTMLUtils.getTagInfo(this.editor,cursor),tokenType=tagInfo.position.tokenType,charCount=0,endQuote="",shouldReplace=!1;return tokenType===HTMLUtils.ATTR_VALUE&&(this.closeOnSelect||null!==completion.match(/\/$/)||(this.closeOnSelect=!0,shouldReplace=!0),tagInfo.attr.hasEndQuote||((endQuote=tagInfo.attr.quoteChar)?completion+=endQuote:0===tagInfo.position.offset&&(completion='"'+completion+'"')),shouldReplace?charCount=tagInfo.attr.value.length:(charCount=this.info.filter.length,this.info.attr.value.length>charCount&&"/"===this.info.attr.value[charCount]&&(charCount+=1))),end.line=start.line=cursor.line,start.ch=cursor.ch-tagInfo.position.offset,end.ch=start.ch+charCount,this.editor.document.replaceRange(completion,start,end),this.closeOnSelect?(tokenType===HTMLUtils.ATTR_VALUE&&tagInfo.attr.hasEndQuote&&this.editor.setCursorPos(start.line,start.ch+completion.length+1),!1):(tokenType!==HTMLUtils.ATTR_VALUE||tagInfo.attr.hasEndQuote||this.editor.setCursorPos(start.line,start.ch+completion.length-1),!0)},AppInit.appReady(function(){data=JSON.parse(Data),htmlAttrs=data.htmlAttrs,urlHints=new UrlCodeHints,CodeHintManager.registerHintProvider(urlHints,["css","html","less","scss"],5),FileSystem.on("change",_clearCachedHints),FileSystem.on("rename",_clearCachedHints),exports.hintProvider=urlHints})});