title: Writing a custom mode for CodeMirror, for use in Brackets
body: <p>I am trying to write a plugin/extension for Brackets that will handle PowerShell.  Well after looking into it, I found that CodeMirror also doesn't have a PowerShell mode, so I need to create it myself.  I am having a terrible time because there are hardly any detailed resources online for what I am trying to do.  </p>
  
  <p><br><br>
  This is my <code>main.js</code> file:</p>
  
  <pre><code>    define(function (require, exports, module){
          "use strict";
  
  
          //Load Modules
          var LanguageManager = brackets.getModule("language/LanguageManager"),
              CodeMirror = brackets.getModule("thirdparty/CodeMirror2/lib/codemirror"),
              PowerShellMode = require("powershell.js");
  
  
          //Define the Language
          LanguageManager.defineLanguage("powershell", {
          name: "PowerShell",
          mode: "powershell",
          fileExtensions: ["ps1"],
          lineComment: ["\/\/"]
          });
  
  
  
          function log(s) {
              console.log("[PS-DevKit] " +s);
          }
  
          log("PowerShell module loaded!");
  
  
      });
  </code></pre>
  
  <p><br><br></p>
  
  <p>This is my <code>powershell.js</code> file:</p>
  
  <pre><code>//CodeMirror Example
  CodeMirror.defineMode("powershell", function() {
  
      return{
          startStat: function() {return {inString: false};},
          token: function(stream, state){
              //If a string starts here
              if (!state.inString &amp;&amp; stream.peek() == '"'){
                  stream.next();              //Skip quote
                  state.inString = true;      //Update state
              }
  
              if (state.inString) {
  
                  if (stream.skipTo('"')){    //Quote found on this line
                      stream.next();          //Skip quote
                      state.inString=false;   //Clear flag
                  } else {
                      stream.skipToEnd();     //Rest of line is string
                  }
  
                  return "red-text";            //Token style
  
              } else {
  
                  stream.skipTo('"') || stream.skipToEnd();
                  return null;                //Unstyled token
  
              }   
          }  
      };    
  });
  </code></pre>
  
  <p><br><br>
  When running Brackets with this code as is, I get an error (developer console) that it could not load my <code>powershell.js</code> file from "Program Files(x86)\Brackets\www".  So I tried putting in the exact path to where the file is (the Bracket extensions folder sitting in my User directory), and it worked but I get the following message:</p>
  
  <pre><code>Use brackets.getModule("thirdparty/CodeMirror2/lib/codemirror") instead of global CodeMirror.
      at Object.defineProperty.get (/brackets.js:115:32)
      at file:///C:/Users/MY_USERNAME/AppData/Roaming/Brackets/extensions/user/PS-DevKit/powershell.js:2:1 
  </code></pre>
  
  <p><br><br>
  Any input?  Right now, all I am trying to do is get it to load and change any text in quotes to red.  Even though I get the deprecation warning about needing to use the CodeMirror module, the extension does load, and if I create a ".ps1" file, it recognizes that it is PowerShell.  </p>