forked from phcode-dev/staging.phcode.dev
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcontinuecomment.js
More file actions
1 lines (1 loc) · 2.81 KB
/
Copy pathcontinuecomment.js
File metadata and controls
1 lines (1 loc) · 2.81 KB
1
!function(mod){"object"==typeof exports&&"object"==typeof module?mod(require("../../lib/codemirror")):"function"==typeof define&&define.amd?define(["../../lib/codemirror"],mod):mod(CodeMirror)}(function(CodeMirror){var nonspace=/\S/g,repeat=String.prototype.repeat||function(n){return Array(n+1).join(this)};function continueComment(cm){if(cm.getOption("disableInput"))return CodeMirror.Pass;for(var ranges=cm.listSelections(),mode,inserts=[],i=0;i<ranges.length;i++){var pos=ranges[i].head;if(!/\bcomment\b/.test(cm.getTokenTypeAt(pos)))return CodeMirror.Pass;var modeHere=cm.getModeAt(pos);if(mode){if(mode!=modeHere)return CodeMirror.Pass}else mode=modeHere;var insert=null,line,found,blockStart=mode.blockCommentStart,lineCmt=mode.lineComment;if(blockStart&&mode.blockCommentContinue){var end=(line=cm.getLine(pos.line)).lastIndexOf(mode.blockCommentEnd,pos.ch-mode.blockCommentEnd.length);if(-1!=end&&end==pos.ch-mode.blockCommentEnd.length||lineCmt&&(found=line.lastIndexOf(lineCmt,pos.ch-1))>-1&&/\bcomment\b/.test(cm.getTokenTypeAt({line:pos.line,ch:found+1})));else if(pos.ch>=blockStart.length&&(found=line.lastIndexOf(blockStart,pos.ch-blockStart.length))>-1&&found>end)if(nonspaceAfter(0,line)>=found)insert=line.slice(0,found);else{var tabSize=cm.options.tabSize,numTabs;found=CodeMirror.countColumn(line,found,tabSize),insert=cm.options.indentWithTabs?repeat.call("\t",numTabs=Math.floor(found/tabSize))+repeat.call(" ",found-tabSize*numTabs):repeat.call(" ",found)}else(found=line.indexOf(mode.blockCommentContinue))>-1&&found<=pos.ch&&found<=nonspaceAfter(0,line)&&(insert=line.slice(0,found));null!=insert&&(insert+=mode.blockCommentContinue)}if(null==insert&&lineCmt&&continueLineCommentEnabled(cm))if(null==line&&(line=cm.getLine(pos.line)),found=line.indexOf(lineCmt),pos.ch||found){if(found>-1&&nonspaceAfter(0,line)>=found){if(!(insert=nonspaceAfter(pos.ch,line)>-1)){var next=cm.getLine(pos.line+1)||"",nextFound=next.indexOf(lineCmt);insert=nextFound>-1&&nonspaceAfter(0,next)>=nextFound||null}insert&&(insert=line.slice(0,found)+lineCmt+line.slice(found+lineCmt.length).match(/^\s*/)[0])}}else insert="";if(null==insert)return CodeMirror.Pass;inserts[i]="\n"+insert}cm.operation(function(){for(var i=ranges.length-1;i>=0;i--)cm.replaceRange(inserts[i],ranges[i].from(),ranges[i].to(),"+insert")})}function nonspaceAfter(ch,str){nonspace.lastIndex=ch;var m=nonspace.exec(str);return m?m.index:-1}function continueLineCommentEnabled(cm){var opt=cm.getOption("continueComments");return!opt||"object"!=typeof opt||!1!==opt.continueLineComment}CodeMirror.defineOption("continueComments",null,function(cm,val,prev){if(prev&&prev!=CodeMirror.Init&&cm.removeKeyMap("continueComment"),val){var key="Enter";"string"==typeof val?key=val:"object"==typeof val&&val.key&&(key=val.key);var map={name:"continueComment"};map[key]=continueComment,cm.addKeyMap(map)}})});