PageSourceSearch

https://0xc0ffee.netlify.app/assets/03-filetype.html-BfGFhv94.js

js 0xc0ffee.netlify.app collected 2026-10-03 10:22:30 UTC 15,218 bytes, 49 lines download raw bytes

1import{_ as s,c as a,e,o as t}from"./app-CjUkjRdW.js";const i="/assets/file-type-CZhWG4PN.png",l="/assets/file-type-in-sandbox-CT4TPGbE.png",p={};function o(c,n){return t(),a("div",null,n[0]||(n[0]=[e('<h1 id="file-type" tabindex="-1"><a class="header-anchor" href="#file-type"><span>File Type</span></a></h1><p>For the IDE to recognize files of a new language, we need to register a new file type. The following diagram shows the relationship between a few classes that are involved in registering a new file type.</p><img src="'+i+`" alt="File Type" width="550"><ul><li>The <code>NimFileType</code> class, which extends <code>LanguageFileType</code>, is responsible for defining the file type, such as the file extension, icon, and language associated with the file type.</li><li>The <code>NimLanguage</code> class, which extends <code>Language</code>, is used to identify the language. It defines the language ID, and optionally a display name, any MIME types associated with the language, and whether the language is case-sensitive.</li></ul><h2 id="file-type-and-language" tabindex="-1"><a class="header-anchor" href="#file-type-and-language"><span>File Type and Language</span></a></h2><p>Let&#39;s start by defining the language class for Nim. This is simply a singleton object that extends <code>Language</code> and defines the language ID as &quot;Nim&quot;.</p><div class="language-kotlin line-numbers-mode" data-highlighter="prismjs" data-ext="kt" data-title="kt"><pre><code><span class="line"><span class="token comment">// src/main/kotlin/khaledh/nimjet/NimLanguage.kt</span></span>
2<span class="line"></span>
3<span class="line"><span class="token keyword">package</span> khaledh<span class="token punctuation">.</span>nimjet</span>
4<span class="line"></span>
5<span class="line"><span class="token keyword">import</span> com<span class="token punctuation">.</span>intellij<span class="token punctuation">.</span>lang<span class="token punctuation">.</span>Language</span>
6<span class="line"></span>
7<span class="line"><span class="token keyword">object</span> NimLanguage <span class="token operator">:</span> <span class="token function">Language</span><span class="token punctuation">(</span><span class="token string-literal singleline"><span class="token string">&quot;Nim&quot;</span></span><span class="token punctuation">)</span></span>
8<span class="line"></span></code></pre><div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0;"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>Next, we define the file type class.</p><div class="language-kotlin line-numbers-mode" data-highlighter="prismjs" data-ext="kt" data-title="kt"><pre><code><span class="line"><span class="token comment">// src/main/kotlin/khaledh/nimjet/NimFileType.kt</span></span>
9<span class="line"></span>
10<span class="line"><span class="token keyword">package</span> khaledh<span class="token punctuation">.</span>nimjet</span>
11<span class="line"></span>
12<span class="line"><span class="token keyword">import</span> com<span class="token punctuation">.</span>intellij<span class="token punctuation">.</span>openapi<span class="token punctuation">.</span>fileTypes<span class="token punctuation">.</span>LanguageFileType</span>
13<span class="line"></span>
14<span class="line"><span class="token keyword">object</span> NimFileType <span class="token operator">:</span> <span class="token function">LanguageFileType</span><span class="token punctuation">(</span>NimLanguage<span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
15<span class="line">    <span class="token keyword">override</span> <span class="token keyword">fun</span> <span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=</span> <span class="token string-literal singleline"><span class="token string">&quot;Nim&quot;</span></span></span>
16<span class="line">    <span class="token keyword">
16override</span> <span class="token keyword">fun</span> <span class="token function">getDescription</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=</span> <span class="token string-literal singleline"><span class="token string">&quot;Nim file&quot;</span></span></span>
17<span class="line">    <span class="token keyword">override</span> <span class="token keyword">fun</span> <span class="token function">getDefaultExtension</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=</span> <span class="token string-literal singleline"><span class="token string">&quot;nim&quot;</span></span></span>
18<span class="line">    <span class="token keyword">override</span> <span class="token keyword">fun</span> <span class="token function">getIcon</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=</span> NimIcons<span class="token punctuation">.</span>FILE</span>
19<span class="line"><span class="token punctuation">}</span></span>
20<span class="line"></span></code></pre><div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0;"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>Let&#39;s break things down:</p><ul><li>We associate the Nim file type with the Nim language instance we created earlier.</li><li>We provide a name and a description for the file type.</li><li>We specify <code>nim</code> as the default extension for files of this type.</li><li>We provide an icon for the file type.</li></ul><p>For the icon, I downloaded the Nim crown logo and placed it in the <code>resources/icons</code> folder. Let&#39;s create the <code>NimIcons</code> object to load the icon (if we need more icons later, we can add them here as well).</p><div class="language-kotlin line-numbers-mode" data-highlighter="prismjs" data-ext="kt" data-title="kt"><pre><code><span class="line"><span class="token comment">// src/main/kotlin/khaledh/nimjet/NimIcons.kt</span></span>
21<span class="line"></span>
22<span class="line"><span class="token keyword">package</span> khaledh<span class="token punctuation">.</span>nimjet</span>
23<span class="line"></span>
24<span class="line"><span class="token keyword">import</span> com<span class="token punctuation">.</span>intellij<span class="token punctuation">.</span>openapi<span class="token punctuation">.</span>util<span class="token punctuation">.</span>IconLoader</span>
25<span class="line"></span>
26<span class="line"><span class="token keyword">object</span> NimIcons <span class="token punctuation">{</span></span>
27<span class="line">    <span class="token keyword">val</span> FILE <span class="token operator">=</span> IconLoader<span class="token punctuation">.</span><span class="token function">getIcon</span><span class="token punctuation">(</span><span class="token string-literal singleline"><span class="token string">&quot;/icons/nim.png&quot;</span></span><span class="token punctuation">,</span> NimIcons<span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">.</span>java<span class="token punctuation">)</span></span>
28<span class="line"><span class="token punctuation">}</span></span>
29<span class="line"></span></code></pre><div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0;"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="registering-the-file-type" tabindex="-1"><a class="header-anchor" href="#registering-the-file-type"><span>Registering the File Type</span></a></h2><p>Finally, we can register the file type in the <code>plugin.xml</code> file under the <code>fileType</code> extension point.</p><div class="language-xml line-numbers-mode" data-highlighter="prismjs" data-ext="xml" data-title="xml"><pre><code><span class="line"><span class="token comment">&lt;!-- src/main/resources/META-INF/plugin.xml --&gt;</span></span>
30<span class="line"></span>
31<span class="line"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>idea-plugin</span><span class="token punctuation">&gt;</span></span></span>
32<span class="line"></span>
33<span class="line">    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>id</span><span class="token punctuation">&gt;</span></span>khaledh.nimjet<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>id</span><span class="token punctuation">&gt;</span></span></span>
34<span class="line"></span>
35<span class="line">    <span class="token comment">&lt;!-- ... --&gt;</span></span>
36<span class="line"></span>
37<span class="line">    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>extensions</span> <span class="token attr-name">defaultExtensionNs</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>com.intellij<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span></span>
38<span class="line"></span>
39<span class="line">
39        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>fileType</span></span>
40<span class="line">            <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>Nim<span class="token punctuation">&quot;</span></span></span>
41<span class="line">            <span class="token attr-name">language</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>Nim<span class="token punctuation">&quot;</span></span></span>
42<span class="line">            <span class="token attr-name">extensions</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>nim<span class="token punctuation">&quot;</span></span></span>
43<span class="line">            <span class="token attr-name">implementationClass</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>khaledh.nimjet.NimFileType<span class="token punctuation">&quot;</span></span></span>
44<span class="line">            <span class="token attr-name">fieldName</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>INSTANCE<span class="token punctuation">&quot;</span></span><span class="token punctuation">/&gt;</span></span></span>
45<span class="line"></span>
46<span class="line">    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>extensions</span><span class="token punctuation">&gt;</span></span></span>
47<span class="line"></span>
48<span class="line"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>idea-plugin</span><span class="token punctuation">&gt;</span></span></span>
49<span class="line"></span></code></pre><div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0;"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>It&#39;s not clear to me why we need to repeat the file type name, language and file extension here, since we&#39;ve already defined them in the <code>NimFileType</code> class. I suspect it may have to do with the need for the IDE to gather all the information it needs about the file type without needing to load the class until it&#39;s actually needed.</p><h2 id="testing-the-file-type" tabindex="-1"><a class="header-anchor" href="#testing-the-file-type"><span>Testing the File Type</span></a></h2><p>Let&#39;s run the sandbox IDE and create a new file with the <code>.nim</code> extension to see if the IDE recognizes it as a Nim file.</p><p><img src="`+l+'" alt="File Type in Sandbox"></p><p>Great! The IDE recognizes the file as a Nim file, and we can see the Nim icon in the file tree as well as the editor tab. We&#39;re now ready to flesh out the plugin with more features.</p><p>Our next goal is to provide syntax highlighting for Nim files. But to get there, we need to implement a lexer first, since syntax highlighting is based on the tokens produced by the lexer. We&#39;ll cover that in the next section.</p>',22)]))}const d=s(p,[["render",o],["__file","03-filetype.html.vue"]]),r=JSON.parse('{"path":"/nimjet/03-filetype.html","title":"File Type","lang":"en-US","frontmatter":{},"headers":[{"level":2,"title":"File Type and Language","slug":"file-type-and-language","link":"#file-type-and-language","children":[]},{"level":2,"title":"Registering the File Type","slug":"registering-the-file-type","link":"#registering-the-file-type","children":[]},{"level":2,"title":"Testing the File Type","slug":"testing-the-file-type","link":"#testing-the-file-type","children":[]}
49],"git":{"updatedTime":1725168053000},"filePathRelative":"nimjet/03-filetype.md","excerpt":"\\n<p>For the IDE to recognize files of a new language, we need to register a new file type. The\\nfollowing diagram shows the relationship between a few classes that are involved in\\nregistering a new file type.</p>\\n\\n<ul>\\n<li>The <code>NimFileType</code> class, which extends <code>LanguageFileType</code>, is responsible for defining\\nthe file type, such as the file extension, icon, and language associated with the file\\ntype.</li>\\n<li>The <code>NimLanguage</code> class, which extends <code>Language</code>, is used to identify the language. It\\ndefines the language ID, and optionally a display name, any MIME types associated with\\nthe language, and whether the language is case-sensitive.</li>\\n</ul>"}');export{d as comp,r as data};

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.