-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmain.js
More file actions
1 lines (1 loc) · 8.19 KB
/
main.js
File metadata and controls
1 lines (1 loc) · 8.19 KB
1
define(function(require,exports,module){var AppInit=brackets.getModule("utils/AppInit"),CodeHintManager=brackets.getModule("editor/CodeHintManager"),PreferencesManager=brackets.getModule("preferences/PreferencesManager"),StringMatch=brackets.getModule("utils/StringMatch"),ExtensionUtils=brackets.getModule("utils/ExtensionUtils"),EditorManager=brackets.getModule("editor/EditorManager"),LanguageManager=brackets.getModule("language/LanguageManager"),JSONUtils=brackets.getModule("language/JSONUtils"),Strings=brackets.getModule("strings"),ThemeManager=brackets.getModule("view/ThemeManager"),CodeInspection=brackets.getModule("language/CodeInspection"),_=brackets.getModule("thirdparty/lodash"),ProjectManager=brackets.getModule("project/ProjectManager"),languages=LanguageManager.getLanguages(),isPrefDocument=!1,isPrefHintsEnabled=!1,data={language:{type:"object",description:Strings.DESCRIPTION_LANGUAGE},path:{type:"object",description:Strings.DESCRIPTION_PATH}},stringMatcherOptions={preferPrefixMatches:!0},parentKeyBlacklist=["language.fileExtensions","language.fileNames","path"];function _isPrefHintsEnabled(){return isPrefDocument&&!1!==PreferencesManager.get("showCodeHints")&&!1!==PreferencesManager.get("codehint.PrefHints")}function _isPrefDocument(document){return/^\.?brackets\.json$/.test(document.file._name)||/^\.?phcode\.json$/.test(document.file._name)}function formatHints(hints,query){var hasMetadata=hints.some(function(token){return token.type||token.description});return StringMatch.basicMatchSort(hints),hints.map(function(token){var $hintItem=$("<span>").addClass("brackets-pref-hints brackets-hints"),$hintObj=$("<span>").addClass("hint-obj");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),$hintItem.append($hintObj),hasMetadata&&($hintItem.data("type",token.type),token.description&&$hintItem.append($("<span>").addClass("hint-description").text(token.description))),$hintItem})}function PrefsCodeHints(){this.ctxInfo=null;var preferences=PreferencesManager.getAllPreferences(),preference;Object.keys(preferences).forEach(function(pref){(preference=preferences[pref]).excludeFromHints||(data[pref]=$.extend(data[pref],preference),preference.keys&&(data[pref].keys=_.clone(preference.keys)))})}PreferencesManager.definePreference("codehint.PrefHints","boolean",!0,{description:Strings.DESCRIPTION_PREF_HINTS}),PreferencesManager.on("change","showCodeHints",function(){isPrefHintsEnabled=_isPrefHintsEnabled()}),PreferencesManager.on("change","codehint.PrefHints",function(){isPrefHintsEnabled=_isPrefHintsEnabled()}),EditorManager.on("activeEditorChange",function(e,editor){editor&&(isPrefDocument=_isPrefDocument(editor.document)),isPrefHintsEnabled=_isPrefHintsEnabled()}),LanguageManager.on("languageAdded",function(){languages=LanguageManager.getLanguages()}),PrefsCodeHints.prototype.hasHints=function(editor,implicitChar){return!!(isPrefHintsEnabled&&"application/json"===editor.getModeForSelection()&&(this.editor=editor,this.ctxInfo=JSONUtils.getContextInfo(this.editor,this.editor.getCursorPos(),!0),this.ctxInfo&&this.ctxInfo.tokenType))&&(this.ctxInfo.tokenType!==JSONUtils.TOKEN_KEY||-1===parentKeyBlacklist.indexOf(this.ctxInfo.parentKeyName))},PrefsCodeHints.prototype.getHints=function(implicitChar){var hints=[],ctxInfo,query,keys,values,option={type:null,description:null,values:null};if((ctxInfo=this.ctxInfo=JSONUtils.getContextInfo(this.editor,this.editor.getCursorPos(),!0))&&ctxInfo.token){if(query=JSONUtils.stripQuotes(ctxInfo.token.string.substr(0,ctxInfo.offset)).trim(),JSONUtils.regexAllowedChars.test(query)&&(query=""),ctxInfo.tokenType===JSONUtils.TOKEN_KEY)data[ctxInfo.parentKeyName]&&data[ctxInfo.parentKeyName].keys?keys=data[ctxInfo.parentKeyName].keys:"language"===ctxInfo.parentKeyName?(keys=languages,option.type="object"):keys=data,hints=$.map(Object.keys(keys),function(key){if(-1===ctxInfo.exclusionList.indexOf(key)){var match=StringMatch.stringMatch(key,query,stringMatcherOptions);if(match)return match.type=keys[key].type||option.type,match.description=keys[key].description||null,match}});else if(ctxInfo.tokenType===JSONUtils.TOKEN_VALUE){if(data[ctxInfo.parentKeyName]&&data[ctxInfo.parentKeyName].keys&&data[ctxInfo.parentKeyName].keys[ctxInfo.keyName]?option=data[ctxInfo.parentKeyName].keys[ctxInfo.keyName]:data[ctxInfo.keyName]&&(option=data[ctxInfo.keyName]),option&&"boolean"===option.type)values=["false","true"];else if(option&&option.values&&(-1!==["number","string"].indexOf(option.type)||"array"===option.type&&ctxInfo.isArray))values=option.values;else if(ctxInfo.isArray&&"linting.prefer"===ctxInfo.keyName&&languages[ctxInfo.parentKeyName])values=CodeInspection.getProviderIDsForLanguage(ctxInfo.parentKeyName);else if("themes.theme"===ctxInfo.keyName)values=ThemeManager.getAllThemes().map(function(theme){return theme.name});else{if("language.fileExtensions"!==ctxInfo.parentKeyName&&"language.fileNames"!==ctxInfo.parentKeyName)return null;values=Object.keys(languages)}"number"!==option.type&&"number"!==option.valueType||(values=values.map(function(val){return val.toString()})),hints=$.map(values,function(value){var match=StringMatch.stringMatch(value,query,stringMatcherOptions);if(match)return match.type=option.valueType||option.type,match.description=option.description||null,match})}return{hints:formatHints(hints,query),match:null,selectInitial:!0,handleWideResults:!1}}return null},PrefsCodeHints.prototype.insertHint=function(completion){var ctxInfo=JSONUtils.getContextInfo(this.editor,this.editor.getCursorPos(),!1,!0),pos=this.editor.getCursorPos(),start={line:-1,ch:-1},end={line:-1,ch:-1},startChar,quoteChar,type;if(completion.jquery&&(type=completion.data("type"),completion=completion.find(".hint-obj").text()),start.line=end.line=pos.line,ctxInfo.tokenType===JSONUtils.TOKEN_KEY){if(startChar=ctxInfo.token.string.charAt(0),/^['"]$/.test(startChar)&&(quoteChar=startChar),completion=quoteChar+completion+quoteChar,!ctxInfo.shouldReplace)switch(completion+=": ",type){case"object":completion+="{}";break;case"array":completion+="[]";break;case"string":completion+='""'}return start.ch=pos.ch-ctxInfo.offset,end.ch=ctxInfo.token.end,this.editor.document.replaceRange(completion,start,end),-1===["object","array","string"].indexOf(type)||(this.editor.setCursorPos(start.line,start.ch+completion.length-1),"object"!==type&&!ctxInfo.shouldReplace)}if(ctxInfo.tokenType===JSONUtils.TOKEN_VALUE)return JSONUtils.regexAllowedChars.test(ctxInfo.token.string)?start.ch=end.ch=pos.ch:ctxInfo.shouldReplace?(start.ch=ctxInfo.token.start,end.ch=ctxInfo.token.end):(start.ch=pos.ch-ctxInfo.offset,end.ch=ctxInfo.token.end),type&&"string"!==type||(startChar=ctxInfo.token.string.charAt(0),completion=(quoteChar=/^['"]$/.test(startChar)?startChar:'"')+completion+quoteChar),this.editor.document.replaceRange(completion,start,end),!1};const ERROR_MESSAGE={errors:[{pos:{line:0,ch:0},message:Strings.ERROR_PREFS_PROJECT_LINT_MESSAGE,type:CodeInspection.Type.ERROR}]},PHOENIX_PREF_FILE="."+PreferencesManager.SETTINGS_FILENAME,BRACKETS_PREF_FILE="."+PreferencesManager.SETTINGS_FILENAME_BRACKETS;async function _hasConflictingPrefFiles(){const root=ProjectManager.getProjectRoot();let bracketsExists=await Phoenix.VFS.existsAsync(root.fullPath+PHOENIX_PREF_FILE),phoenixExists;if(await Phoenix.VFS.existsAsync(root.fullPath+BRACKETS_PREF_FILE)&&bracketsExists)return!0}function lintOneFile(text,fullPath){return new Promise(resolve=>{const root=ProjectManager.getProjectRoot(),fileName=fullPath.replace(root.fullPath,"");fileName===PHOENIX_PREF_FILE||fileName===BRACKETS_PREF_FILE?_hasConflictingPrefFiles().then(isConflicting=>{resolve(isConflicting?ERROR_MESSAGE:void 0)}):resolve()})}function _setupTestEnvironment(testDocument,testPreferences){isPrefHintsEnabled=_isPrefDocument(testDocument),data=testPreferences}AppInit.appReady(function(){var hintProvider=new PrefsCodeHints;CodeHintManager.registerHintProvider(hintProvider,["json"],0),ExtensionUtils.loadStyleSheet(module,"styles/brackets-prefs-hints.css"),Phoenix.isSpecRunnerWindow||CodeInspection.register("json",{name:Strings.ERROR_PREFS_PROJECT_LINT,scanFileAsync:lintOneFile}),exports.hintProvider=hintProvider,exports._setupTestEnvironment=_setupTestEnvironment})});