|
|
<!DOCTYPE HTML>
|
|
|
<html lang="en" class="colibri" dir="ltr">
|
|
|
<head>
|
|
|
<!-- Book generated using mdBook -->
|
|
|
<meta charset="UTF-8">
|
|
|
<title>Adding indent queries</title>
|
|
|
|
|
|
|
|
|
<!-- Custom HTML head -->
|
|
|
|
|
|
<meta name="description" content="">
|
|
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
|
<meta name="theme-color" content="#ffffff">
|
|
|
|
|
|
<link rel="icon" href="../favicon.svg">
|
|
|
<link rel="shortcut icon" href="../favicon.png">
|
|
|
<link rel="stylesheet" href="../css/variables.css">
|
|
|
<link rel="stylesheet" href="../css/general.css">
|
|
|
<link rel="stylesheet" href="../css/chrome.css">
|
|
|
<link rel="stylesheet" href="../css/print.css" media="print">
|
|
|
|
|
|
<!-- Fonts -->
|
|
|
<link rel="stylesheet" href="../FontAwesome/css/font-awesome.css">
|
|
|
<link rel="stylesheet" href="../fonts/fonts.css">
|
|
|
|
|
|
<!-- Highlight.js Stylesheets -->
|
|
|
<link rel="stylesheet" href="../highlight.css">
|
|
|
<link rel="stylesheet" href="../tomorrow-night.css">
|
|
|
<link rel="stylesheet" href="../ayu-highlight.css">
|
|
|
|
|
|
<!-- Custom theme stylesheets -->
|
|
|
<link rel="stylesheet" href="../custom.css">
|
|
|
|
|
|
</head>
|
|
|
<body class="sidebar-visible no-js">
|
|
|
<div id="body-container">
|
|
|
<!-- Provide site root to javascript -->
|
|
|
<script>
|
|
|
var path_to_root = "../";
|
|
|
var default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "colibri" : "colibri";
|
|
|
</script>
|
|
|
|
|
|
<!-- Work around some values being stored in localStorage wrapped in quotes -->
|
|
|
<script>
|
|
|
try {
|
|
|
var theme = localStorage.getItem('mdbook-theme');
|
|
|
var sidebar = localStorage.getItem('mdbook-sidebar');
|
|
|
|
|
|
if (theme.startsWith('"') && theme.endsWith('"')) {
|
|
|
localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
|
|
|
}
|
|
|
|
|
|
if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
|
|
|
localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
|
|
|
}
|
|
|
} catch (e) { }
|
|
|
</script>
|
|
|
|
|
|
<!-- Set the theme before any content is loaded, prevents flash -->
|
|
|
<script>
|
|
|
var theme;
|
|
|
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
|
|
|
if (theme === null || theme === undefined) { theme = default_theme; }
|
|
|
var html = document.querySelector('html');
|
|
|
html.classList.remove('colibri')
|
|
|
html.classList.add(theme);
|
|
|
var body = document.querySelector('body');
|
|
|
body.classList.remove('no-js')
|
|
|
body.classList.add('js');
|
|
|
</script>
|
|
|
|
|
|
<input type="checkbox" id="sidebar-toggle-anchor" class="hidden">
|
|
|
|
|
|
<!-- Hide / unhide sidebar before it is displayed -->
|
|
|
<script>
|
|
|
var body = document.querySelector('body');
|
|
|
var sidebar = null;
|
|
|
var sidebar_toggle = document.getElementById("sidebar-toggle-anchor");
|
|
|
if (document.body.clientWidth >= 1080) {
|
|
|
try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
|
|
|
sidebar = sidebar || 'visible';
|
|
|
} else {
|
|
|
sidebar = 'hidden';
|
|
|
}
|
|
|
sidebar_toggle.checked = sidebar === 'visible';
|
|
|
body.classList.remove('sidebar-visible');
|
|
|
body.classList.add("sidebar-" + sidebar);
|
|
|
</script>
|
|
|
|
|
|
<nav id="sidebar" class="sidebar" aria-label="Table of contents">
|
|
|
<div class="sidebar-scrollbox">
|
|
|
<ol class="chapter"><li class="chapter-item expanded affix "><a href="../title-page.html">Helix</a></li><li class="chapter-item expanded "><a href="../install.html"><strong aria-hidden="true">1.</strong> Installation</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="../package-managers.html"><strong aria-hidden="true">1.1.</strong> Package Managers</a></li><li class="chapter-item expanded "><a href="../building-from-source.html"><strong aria-hidden="true">1.2.</strong> Building from source</a></li></ol></li><li class="chapter-item expanded "><a href="../usage.html"><strong aria-hidden="true">2.</strong> Usage</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="../registers.html"><strong aria-hidden="true">2.1.</strong> Registers</a></li><li class="chapter-item expanded "><a href="../surround.html"><strong aria-hidden="true">2.2.</strong> Surround</a></li><li class="chapter-item expanded "><a href="../textobjects.html"><strong aria-hidden="true">2.3.</strong> Textobjects</a></li><li class="chapter-item expanded "><a href="../syntax-aware-motions.html"><strong aria-hidden="true">2.4.</strong> Syntax aware motions</a></li><li class="chapter-item expanded "><a href="../pickers.html"><strong aria-hidden="true">2.5.</strong> Pickers</a></li><li class="chapter-item expanded "><a href="../keymap.html"><strong aria-hidden="true">2.6.</strong> Keymap</a></li><li class="chapter-item expanded "><a href="../commands.html"><strong aria-hidden="true">2.7.</strong> Commands</a></li><li class="chapter-item expanded "><a href="../lang-support.html"><strong aria-hidden="true">2.8.</strong> Language support</a></li></ol></li><li class="chapter-item expanded "><a href="../from-vim.html"><strong aria-hidden="true">3.</strong> Migrating from Vim</a></li><li class="chapter-item expanded "><a href="../configuration.html"><strong aria-hidden="true">4.</strong> Configuration</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="../editor.html"><strong aria-hidden="true">4.1.</strong> Editor</a></li><li class="chapter-item expanded "><a href="../themes.html"><strong aria-hidden="true">4.2.</strong> Themes</a></li><li class="chapter-item expanded "><a href="../remapping.html"><strong aria-hidden="true">4.3.</strong> Key remapping</a></li><li class="chapter-item expanded "><a href="../languages.html"><strong aria-hidden="true">4.4.</strong> Languages</a></li></ol></li><li class="chapter-item expanded "><a href="../guides/index.html"><strong aria-hidden="true">5.</strong> Guides</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="../guides/adding_languages.html"><strong aria-hidden="true">5.1.</strong> Adding languages</a></li><li class="chapter-item expanded "><a href="../guides/textobject.html"><strong aria-hidden="true">5.2.</strong> Adding textobject queries</a></li><li class="chapter-item expanded "><a href="../guides/indent.html" class="active"><strong aria-hidden="true">5.3.</strong> Adding indent queries</a></li><li class="chapter-item expanded "><a href="../guides/injection.html"><strong aria-hidden="true">5.4.</strong> Adding injection queries</a></li></ol></li></ol>
|
|
|
</div>
|
|
|
<div id="sidebar-resize-handle" class="sidebar-resize-handle"></div>
|
|
|
</nav>
|
|
|
|
|
|
<!-- Track and set sidebar scroll position -->
|
|
|
<script>
|
|
|
var sidebarScrollbox = document.querySelector('#sidebar .sidebar-scrollbox');
|
|
|
sidebarScrollbox.addEventListener('click', function(e) {
|
|
|
if (e.target.tagName === 'A') {
|
|
|
sessionStorage.setItem('sidebar-scroll', sidebarScrollbox.scrollTop);
|
|
|
}
|
|
|
}, { passive: true });
|
|
|
var sidebarScrollTop = sessionStorage.getItem('sidebar-scroll');
|
|
|
sessionStorage.removeItem('sidebar-scroll');
|
|
|
if (sidebarScrollTop) {
|
|
|
// preserve sidebar scroll position when navigating via links within sidebar
|
|
|
sidebarScrollbox.scrollTop = sidebarScrollTop;
|
|
|
} else {
|
|
|
// scroll sidebar to current active section when navigating via "next/previous chapter" buttons
|
|
|
var activeSection = document.querySelector('#sidebar .active');
|
|
|
if (activeSection) {
|
|
|
activeSection.scrollIntoView({ block: 'center' });
|
|
|
}
|
|
|
}
|
|
|
</script>
|
|
|
|
|
|
<div id="page-wrapper" class="page-wrapper">
|
|
|
|
|
|
<div class="page">
|
|
|
<div id="menu-bar-hover-placeholder"></div>
|
|
|
<div id="menu-bar" class="menu-bar sticky">
|
|
|
<div class="left-buttons">
|
|
|
<label id="sidebar-toggle" class="icon-button" for="sidebar-toggle-anchor" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
|
|
|
<i class="fa fa-bars"></i>
|
|
|
</label>
|
|
|
<button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list">
|
|
|
<i class="fa fa-paint-brush"></i>
|
|
|
</button>
|
|
|
<ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
|
|
|
<li role="none"><button role="menuitem" class="theme" id="light">Light</button></li>
|
|
|
<li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
|
|
|
<li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
|
|
|
<li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
|
|
|
<li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
|
|
|
<li role="none"><button role="menuitem" class="theme" id="colibri">Colibri</button></li>
|
|
|
</ul>
|
|
|
<button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
|
|
|
<i class="fa fa-search"></i>
|
|
|
</button>
|
|
|
</div>
|
|
|
|
|
|
<h1 class="menu-title"></h1>
|
|
|
|
|
|
<div class="right-buttons">
|
|
|
<a href="../print.html" title="Print this book" aria-label="Print this book">
|
|
|
<i id="print-button" class="fa fa-print"></i>
|
|
|
</a>
|
|
|
<a href="https://github.com/helix-editor/helix" title="Git repository" aria-label="Git repository">
|
|
|
<i id="git-repository-button" class="fa fa-github"></i>
|
|
|
</a>
|
|
|
<a href="https://github.com/helix-editor/helix/edit/master/book/src/guides/indent.md" title="Suggest an edit" aria-label="Suggest an edit">
|
|
|
<i id="git-edit-button" class="fa fa-edit"></i>
|
|
|
</a>
|
|
|
|
|
|
</div>
|
|
|
</div>
|
|
|
|
|
|
<div id="search-wrapper" class="hidden">
|
|
|
<form id="searchbar-outer" class="searchbar-outer">
|
|
|
<input type="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
|
|
|
</form>
|
|
|
<div id="searchresults-outer" class="searchresults-outer hidden">
|
|
|
<div id="searchresults-header" class="searchresults-header"></div>
|
|
|
<ul id="searchresults">
|
|
|
</ul>
|
|
|
</div>
|
|
|
</div>
|
|
|
|
|
|
<!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
|
|
|
<script>
|
|
|
document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
|
|
|
document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
|
|
|
Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
|
|
|
link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
|
|
|
});
|
|
|
</script>
|
|
|
|
|
|
<div id="content" class="content">
|
|
|
<main>
|
|
|
<h2 id="adding-indent-queries"><a class="header" href="#adding-indent-queries">Adding indent queries</a></h2>
|
|
|
<p>Helix uses tree-sitter to correctly indent new lines. This requires a tree-
|
|
|
sitter grammar and an <code>indent.scm</code> query file placed in <code>runtime/queries/ {language}/indents.scm</code>. The indentation for a line is calculated by traversing
|
|
|
the syntax tree from the lowest node at the beginning of the new line (see
|
|
|
<a href="#indent-queries">Indent queries</a>). Each of these nodes contributes to the total
|
|
|
indent when it is captured by the query (in what way depends on the name of
|
|
|
the capture.</p>
|
|
|
<p>Note that it matters where these added indents begin. For example,
|
|
|
multiple indent level increases that start on the same line only increase
|
|
|
the total indent level by 1. See <a href="#capture-types">Capture types</a>.</p>
|
|
|
<p>By default, Helix uses the <code>hybrid</code> indentation heuristic. This means that
|
|
|
indent queries are not used to compute the expected absolute indentation of a
|
|
|
line but rather the expected difference in indentation between the new and an
|
|
|
already existing line. This difference is then added to the actual indentation
|
|
|
of the already existing line. Since this makes errors in the indent queries
|
|
|
harder to find, it is recommended to disable it when testing via
|
|
|
<code>:set indent-heuristic tree-sitter</code>. The rest of this guide assumes that
|
|
|
the <code>tree-sitter</code> heuristic is used.</p>
|
|
|
<h2 id="indent-queries"><a class="header" href="#indent-queries">Indent queries</a></h2>
|
|
|
<p>When Helix is inserting a new line through <code>o</code>, <code>O</code>, or <code><ret></code>, to determine
|
|
|
the indent level for the new line, the query in <code>indents.scm</code> is run on the
|
|
|
document. The starting position of the query is the end of the line above where
|
|
|
a new line will be inserted.</p>
|
|
|
<p>For <code>o</code>, the inserted line is the line below the cursor, so that starting
|
|
|
position of the query is the end of the current line.</p>
|
|
|
<pre><pre class="playground"><code class="language-rust"><span class="boring">#![allow(unused)]
|
|
|
</span><span class="boring">fn main() {
|
|
|
</span>fn need_hero(some_hero: Hero, life: Life) -> {
|
|
|
matches!(some_hero, Hero { // ←─────────────────╮
|
|
|
strong: true,//←╮ ↑ ↑ │
|
|
|
fast: true, // │ │ ╰── query start │
|
|
|
sure: true, // │ ╰───── cursor ├─ traversal
|
|
|
soon: true, // ╰──────── new line inserted │ start node
|
|
|
}) && // │
|
|
|
// ↑ │
|
|
|
// ╰───────────────────────────────────────────────╯
|
|
|
some_hero > life
|
|
|
}
|
|
|
<span class="boring">}</span></code></pre></pre>
|
|
|
<p>For <code>O</code>, the newly inserted line is the <em>current</em> line, so the starting position
|
|
|
of the query is the end of the line above the cursor.</p>
|
|
|
<pre><pre class="playground"><code class="language-rust"><span class="boring">#![allow(unused)]
|
|
|
</span><span class="boring">fn main() {
|
|
|
</span>fn need_hero(some_hero: Hero, life: Life) -> { // ←─╮
|
|
|
matches!(some_hero, Hero { // ←╮ ↑ │
|
|
|
strong: true,// ↑ ╭───╯ │ │
|
|
|
fast: true, // │ │ query start ─╯ │
|
|
|
sure: true, // ╰───┼ cursor ├─ traversal
|
|
|
soon: true, // ╰ new line inserted │ start node
|
|
|
}) && // │
|
|
|
some_hero > life // │
|
|
|
} // ←──────────────────────────────────────────────╯
|
|
|
<span class="boring">}</span></code></pre></pre>
|
|
|
<p>From this starting node, the syntax tree is traversed up until the root node.
|
|
|
Each indent capture is collected along the way, and then combined according to
|
|
|
their <a href="#capture-types">capture types</a> and <a href="#scopes">scopes</a> to a final indent
|
|
|
level for the line.</p>
|
|
|
<h3 id="capture-types"><a class="header" href="#capture-types">Capture types</a></h3>
|
|
|
<ul>
|
|
|
<li><code>@indent</code> (default scope <code>tail</code>):
|
|
|
Increase the indent level by 1. Multiple occurrences in the same line <em>do not</em>
|
|
|
stack. If there is at least one <code>@indent</code> and one <code>@outdent</code> capture on the
|
|
|
same line, the indent level isn't changed at all.</li>
|
|
|
<li><code>@outdent</code> (default scope <code>all</code>):
|
|
|
Decrease the indent level by 1. The same rules as for <code>@indent</code> apply.</li>
|
|
|
<li><code>@indent.always</code> (default scope <code>tail</code>):
|
|
|
Increase the indent level by 1. Multiple occurrences on the same line <em>do</em>
|
|
|
stack. The final indent level is <code>@indent.always</code> – <code>@outdent.always</code>. If
|
|
|
an <code>@indent</code> and an <code>@indent.always</code> are on the same line, the <code>@indent</code> is
|
|
|
ignored.</li>
|
|
|
<li><code>@outdent.always</code> (default scope <code>all</code>):
|
|
|
Decrease the indent level by 1. The same rules as for <code>@indent.always</code> apply.</li>
|
|
|
<li><code>@align</code> (default scope <code>all</code>):
|
|
|
Align everything inside this node to some anchor. The anchor is given
|
|
|
by the start of the node captured by <code>@anchor</code> in the same pattern.
|
|
|
Every pattern with an <code>@align</code> should contain exactly one <code>@anchor</code>.
|
|
|
Indent (and outdent) for nodes below (in terms of their starting line)
|
|
|
the <code>@align</code> node is added to the indentation required for alignment.</li>
|
|
|
<li><code>@extend</code>:
|
|
|
Extend the range of this node to the end of the line and to lines that are
|
|
|
indented more than the line that this node starts on. This is useful for
|
|
|
languages like Python, where for the purpose of indentation some nodes (like
|
|
|
functions or classes) should also contain indented lines that follow them.</li>
|
|
|
<li><code>@extend.prevent-once</code>:
|
|
|
Prevents the first extension of an ancestor of this node. For example, in Python
|
|
|
a return expression always ends the block that it is in. Note that this only
|
|
|
stops the extension of the next <code>@extend</code> capture. If multiple ancestors are
|
|
|
captured, only the extension of the innermost one is prevented. All other
|
|
|
ancestors are unaffected (regardless of whether the innermost ancestor would
|
|
|
actually have been extended).</li>
|
|
|
</ul>
|
|
|
<h4 id="indent--outdent"><a class="header" href="#indent--outdent"><code>@indent</code> / <code>@outdent</code></a></h4>
|
|
|
<p>Consider this example:</p>
|
|
|
<pre><pre class="playground"><code class="language-rust"><span class="boring">#![allow(unused)]
|
|
|
</span><span class="boring">fn main() {
|
|
|
</span>fn shout(things: Vec<Thing>) {
|
|
|
// ↑
|
|
|
// ├───────────────────────╮ indent level
|
|
|
// @indent ├┄┄┄┄┄┄┄┄┄┄┄┄┄┄
|
|
|
// │
|
|
|
let it_all = |out| { things.filter(|thing| { // │ 1
|
|
|
// ↑ ↑ │
|
|
|
// ├───────────────────────┼─────┼┄┄┄┄┄┄┄┄┄┄┄┄┄┄
|
|
|
// @indent @indent │
|
|
|
// │ 2
|
|
|
thing.can_do_with(out) // │
|
|
|
})}; // ├┄┄┄┄┄┄┄┄┄┄┄┄┄┄
|
|
|
//↑↑↑ │ 1
|
|
|
} //╰┼┴──────────────────────────────────────────────┴┄┄┄┄┄┄┄┄┄┄┄┄┄┄
|
|
|
// 3x @outdent
|
|
|
<span class="boring">}</span></code></pre></pre>
|
|
|
<pre><code class="language-scm">((block) @indent)
|
|
|
["}" ")"] @outdent
|
|
|
</code></pre>
|
|
|
<p>Note how on the second line, we have two blocks begin on the same line. In this
|
|
|
case, since both captures occur on the same line, they are combined and only
|
|
|
result in a net increase of 1. Also note that the closing <code>}</code>s are part of the
|
|
|
<code>@indent</code> captures, but the 3 <code>@outdent</code>s also combine into 1 and result in that
|
|
|
line losing one indent level.</p>
|
|
|
<h4 id="extend--extendprevent-once"><a class="header" href="#extend--extendprevent-once"><code>@extend</code> / <code>@extend.prevent-once</code></a></h4>
|
|
|
<p>For an example of where <code>@extend</code> can be useful, consider Python, which is
|
|
|
whitespace-sensitive.</p>
|
|
|
<pre><code class="language-scm">]
|
|
|
(parenthesized_expression)
|
|
|
(function_definition)
|
|
|
(class_definition)
|
|
|
] @indent
|
|
|
|
|
|
</code></pre>
|
|
|
<pre><code class="language-python">class Hero:
|
|
|
def __init__(self, strong, fast, sure, soon):# ←─╮
|
|
|
self.is_strong = strong # │
|
|
|
self.is_fast = fast # ╭─── query start │
|
|
|
self.is_sure = sure # │ ╭─ cursor │
|
|
|
self.is_soon = soon # │ │ │
|
|
|
# ↑ ↑ │ │ │
|
|
|
# │ ╰──────╯ │ │
|
|
|
# ╰─────────────────────╯ │
|
|
|
# ├─ traversal
|
|
|
def need_hero(self, life): # │ start node
|
|
|
return ( # │
|
|
|
self.is_strong # │
|
|
|
and self.is_fast # │
|
|
|
and self.is_sure # │
|
|
|
and self.is_soon # │
|
|
|
and self > life # │
|
|
|
) # ←─────────────────────────────────────────╯
|
|
|
</code></pre>
|
|
|
<p>Without braces to catch the scope of the function, the smallest descendant of
|
|
|
the cursor on a line feed ends up being the entire inside of the class. Because
|
|
|
of this, it will miss the entire function node and its indent capture, leading
|
|
|
to an indent level one too small.</p>
|
|
|
<p>To address this case, <code>@extend</code> tells helix to "extend" the captured node's span
|
|
|
to the line feed and every consecutive line that has a greater indent level than
|
|
|
the line of the node.</p>
|
|
|
<pre><code class="language-scm">(parenthesized_expression) @indent
|
|
|
|
|
|
]
|
|
|
(function_definition)
|
|
|
(class_definition)
|
|
|
] @indent @extend
|
|
|
|
|
|
</code></pre>
|
|
|
<pre><code class="language-python">class Hero:
|
|
|
def __init__(self, strong, fast, sure, soon):# ←─╮
|
|
|
self.is_strong = strong # │
|
|
|
self.is_fast = fast # ╭─── query start ├─ traversal
|
|
|
self.is_sure = sure # │ ╭─ cursor │ start node
|
|
|
self.is_soon = soon # │ │ ←───────────────╯
|
|
|
# ↑ ↑ │ │
|
|
|
# │ ╰──────╯ │
|
|
|
# ╰─────────────────────╯
|
|
|
def need_hero(self, life):
|
|
|
return (
|
|
|
self.is_strong
|
|
|
and self.is_fast
|
|
|
and self.is_sure
|
|
|
and self.is_soon
|
|
|
and self > life
|
|
|
)
|
|
|
</code></pre>
|
|
|
<p>Furthermore, there are some cases where extending to everything with a greater
|
|
|
indent level may not be desirable. Consider the <code>need_hero</code> function above. If
|
|
|
our cursor is on the last line of the returned expression.</p>
|
|
|
<pre><code class="language-python">class Hero:
|
|
|
def __init__(self, strong, fast, sure, soon):
|
|
|
self.is_strong = strong
|
|
|
self.is_fast = fast
|
|
|
self.is_sure = sure
|
|
|
self.is_soon = soon
|
|
|
|
|
|
def need_hero(self, life):
|
|
|
return (
|
|
|
self.is_strong
|
|
|
and self.is_fast
|
|
|
and self.is_sure
|
|
|
and self.is_soon
|
|
|
and self > life
|
|
|
) # ←─── cursor
|
|
|
#←────────── where cursor should go on new line
|
|
|
</code></pre>
|
|
|
<p>In Python, the are a few tokens that will always end a scope, such as a return
|
|
|
statement. Since the scope ends, so should the indent level. But because the
|
|
|
function span is extended to every line with a greater indent level, a new line
|
|
|
would just continue on the same level. And an <code>@outdent</code> would not help us here
|
|
|
either, since it would cause everything in the parentheses to become outdented
|
|
|
as well.</p>
|
|
|
<p>To help, we need to signal an end to the extension. We can do this with
|
|
|
<code>@extend.prevent-once</code>.</p>
|
|
|
<pre><code class="language-scm">(parenthesized_expression) @indent
|
|
|
|
|
|
]
|
|
|
(function_definition)
|
|
|
(class_definition)
|
|
|
] @indent @extend
|
|
|
|
|
|
(return_statement) @extend.prevent-once
|
|
|
</code></pre>
|
|
|
<h4 id="indentalways--outdentalways"><a class="header" href="#indentalways--outdentalways"><code>@indent.always</code> / <code>@outdent.always</code></a></h4>
|
|
|
<p>As mentioned before, normally if there is more than one <code>@indent</code> or <code>@outdent</code>
|
|
|
capture on the same line, they are combined.</p>
|
|
|
<p>Sometimes, there are cases when you may want to ensure that every indent capture
|
|
|
is additive, regardless of how many occur on the same line. Consider this
|
|
|
example in YAML.</p>
|
|
|
<pre><code class="language-yaml"> - foo: bar
|
|
|
# ↑ ↑
|
|
|
# │ ╰─────────────── start of map
|
|
|
# ╰───────────────── start of list element
|
|
|
baz: quux # ←─── cursor
|
|
|
# ←───────────── where the cursor should go on a new line
|
|
|
garply: waldo
|
|
|
- quux:
|
|
|
bar: baz
|
|
|
xyzzy: thud
|
|
|
fred: plugh
|
|
|
</code></pre>
|
|
|
<p>In YAML, you often have lists of maps. In these cases, the syntax is such that
|
|
|
the list element and the map both start on the same line. But we really do want
|
|
|
to start an indentation for each of these so that subsequent keys in the map
|
|
|
hang over the list and align properly. This is where <code>@indent.always</code> helps.</p>
|
|
|
<pre><code class="language-scm">((block_sequence_item) @item @indent.always @extend
|
|
|
(#not-one-line? @item))
|
|
|
|
|
|
((block_mapping_pair
|
|
|
key: (_) @key
|
|
|
value: (_) @val
|
|
|
(#not-same-line? @key @val)
|
|
|
) @indent.always @extend
|
|
|
)
|
|
|
</code></pre>
|
|
|
<h2 id="predicates"><a class="header" href="#predicates">Predicates</a></h2>
|
|
|
<p>In some cases, an S-expression cannot express exactly what pattern should be matched.
|
|
|
For that, tree-sitter allows for predicates to appear anywhere within a pattern,
|
|
|
similar to how <code>#set!</code> declarations work:</p>
|
|
|
<pre><code class="language-scm">(some_kind
|
|
|
(child_kind) @indent
|
|
|
(#predicate? arg1 arg2 ...)
|
|
|
)
|
|
|
</code></pre>
|
|
|
<p>The number of arguments depends on the predicate that's used.
|
|
|
Each argument is either a capture (<code>@name</code>) or a string (<code>"some string"</code>).
|
|
|
The following predicates are supported by tree-sitter:</p>
|
|
|
<ul>
|
|
|
<li>
|
|
|
<p><code>#eq?</code>/<code>#not-eq?</code>:
|
|
|
The first argument (a capture) must/must not be equal to the second argument
|
|
|
(a capture or a string).</p>
|
|
|
</li>
|
|
|
<li>
|
|
|
<p><code>#match?</code>/<code>#not-match?</code>:
|
|
|
The first argument (a capture) must/must not match the regex given in the
|
|
|
second argument (a string).</p>
|
|
|
</li>
|
|
|
<li>
|
|
|
<p><code>#any-of?</code>/<code>#not-any-of?</code>:
|
|
|
The first argument (a capture) must/must not be one of the other arguments
|
|
|
(strings).</p>
|
|
|
</li>
|
|
|
</ul>
|
|
|
<p>Additionally, we support some custom predicates for indent queries:</p>
|
|
|
<ul>
|
|
|
<li>
|
|
|
<p><code>#not-kind-eq?</code>:
|
|
|
The kind of the first argument (a capture) must not be equal to the second
|
|
|
argument (a string).</p>
|
|
|
</li>
|
|
|
<li>
|
|
|
<p><code>#same-line?</code>/<code>#not-same-line?</code>:
|
|
|
The captures given by the 2 arguments must/must not start on the same line.</p>
|
|
|
</li>
|
|
|
<li>
|
|
|
<p><code>#one-line?</code>/<code>#not-one-line?</code>:
|
|
|
The captures given by the fist argument must/must span a total of one line.</p>
|
|
|
</li>
|
|
|
</ul>
|
|
|
<h3 id="scopes"><a class="header" href="#scopes">Scopes</a></h3>
|
|
|
<p>Added indents don't always apply to the whole node. For example, in most
|
|
|
cases when a node should be indented, we actually only want everything
|
|
|
except for its first line to be indented. For this, there are several
|
|
|
scopes (more scopes may be added in the future if required):</p>
|
|
|
<ul>
|
|
|
<li><code>tail</code>:
|
|
|
This scope applies to everything except for the first line of the
|
|
|
captured node.</li>
|
|
|
<li><code>all</code>:
|
|
|
This scope applies to the whole captured node. This is only different from
|
|
|
<code>tail</code> when the captured node is the first node on its line.</li>
|
|
|
</ul>
|
|
|
<p>For example, imagine we have the following function</p>
|
|
|
<pre><pre class="playground"><code class="language-rust"><span class="boring">#![allow(unused)]
|
|
|
</span><span class="boring">fn main() {
|
|
|
</span>fn aha() { // ←─────────────────────────────────────╮
|
|
|
let take = "on me"; // ←──────────────╮ scope: │
|
|
|
let take = "me on"; // ├─ "tail" ├─ (block) @indent
|
|
|
let ill = be_gone_days(1 || 2); // │ │
|
|
|
} // ←───────────────────────────────────┴──────────┴─ "}" @outdent
|
|
|
// scope: "all"
|
|
|
<span class="boring">}</span></code></pre></pre>
|
|
|
<p>We can write the following query with the <code>#set!</code> declaration:</p>
|
|
|
<pre><code class="language-scm">((block) @indent
|
|
|
(#set! "scope" "tail"))
|
|
|
("}" @outdent
|
|
|
(#set! "scope" "all"))
|
|
|
</code></pre>
|
|
|
<p>As we can see, the "tail" scope covers the node, except for the first line.
|
|
|
Everything up to and including the closing brace gets an indent level of 1.
|
|
|
Then, on the closing brace, we encounter an outdent with a scope of "all", which
|
|
|
means the first line is included, and the indent level is cancelled out on this
|
|
|
line. (Note these scopes are the defaults for <code>@indent</code> and <code>@outdent</code>—they are
|
|
|
written explicitly for demonstration.)</p>
|
|
|
|
|
|
</main>
|
|
|
|
|
|
<nav class="nav-wrapper" aria-label="Page navigation">
|
|
|
<!-- Mobile navigation buttons -->
|
|
|
<a rel="prev" href="../guides/textobject.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
|
|
|
<i class="fa fa-angle-left"></i>
|
|
|
</a>
|
|
|
|
|
|
<a rel="next prefetch" href="../guides/injection.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
|
|
|
<i class="fa fa-angle-right"></i>
|
|
|
</a>
|
|
|
|
|
|
<div style="clear: both"></div>
|
|
|
</nav>
|
|
|
</div>
|
|
|
</div>
|
|
|
|
|
|
<nav class="nav-wide-wrapper" aria-label="Page navigation">
|
|
|
<a rel="prev" href="../guides/textobject.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
|
|
|
<i class="fa fa-angle-left"></i>
|
|
|
</a>
|
|
|
|
|
|
<a rel="next prefetch" href="../guides/injection.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
|
|
|
<i class="fa fa-angle-right"></i>
|
|
|
</a>
|
|
|
</nav>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<script>
|
|
|
window.playground_copyable = true;
|
|
|
</script>
|
|
|
|
|
|
|
|
|
<script src="../elasticlunr.min.js"></script>
|
|
|
<script src="../mark.min.js"></script>
|
|
|
<script src="../searcher.js"></script>
|
|
|
|
|
|
<script src="../clipboard.min.js"></script>
|
|
|
<script src="../highlight.js"></script>
|
|
|
<script src="../book.js"></script>
|
|
|
|
|
|
<!-- Custom JS scripts -->
|
|
|
|
|
|
|
|
|
</div>
|
|
|
</body>
|
|
|
</html>
|