Skip to content

Commit

Permalink
Rewrite search
Browse files Browse the repository at this point in the history
Prior to this commit, SDoc's search algorithm was implemented by
[`searcher.js`][].  `searcher.js` builds a regular expression for each
token in the query.  For example, the query "foo bar" generates the
regular expressions `/([f])([^f]*?)([o])([^o]*?)([o])([^o]*?)/i` and
`/([b])([^b]*?)([a])([^a]*?)([r])([^r]*?)/i`.  These regular expressions
fuzzy match missing letters, but fail for any other kind of typo, such
as added letters or swapped letters.  They can also produce surprising
results.  For example, the query "ActiveRecord::Base" returns
`ActiveRecord::AttributeAssignment` as the top result due to matching
"activerecord" attri"b"ute"a"s"s"ignm"e"ent, and there are six(!) other
results that appear before `ActiveRecord::Base`.

This commit implements a new search algorithm based on character-level
bigrams.  For example, the query "foo bar" will look for results that
match "fo", "oo", "o ", " b", "ba", and "ar".  Shorthand bigrams for
CamelCase names are also included in the search index.  For example,
entries containing "ActiveRecord" are also associated with the bigram
"ar".  Bigrams are weighted such that some contribute more to the match
score, and results are ordered by match score.

Here are some example queries and their top results with
rails/rails@7c65a4b both before and
after this commit:

* "ActiveRecord::Base"
  * top result before: `ActiveRecord::AttributeAssignment`
  * top result after: `ActiveRecord::Base`

* "ar base"
  * top result before: `Rails::Generators::Testing::Behavior::ClassMethods#arguments`
  * top result after: `ActiveRecord::Base`

* "hasmany"
  * top result before: `ActiveRecord::Associations::ClassMethods#has_and_belongs_to_many`
  * top result after: `ActiveRecord::Associations::ClassMethods#has_many`

* "adress"
  * top result before: `ActiveSupport::HashWithIndifferentAccess`
  * top result after: `Mail::Address`

* "existance"
  * top result before: no results
  * top result after: `Pathname#existence`

* "foriegn"
  * top result before: no results
  * top result after: `String#foreign_key`

This commit also redesigns the presentation of search results.  Prior to
this commit, result names were cut off at ~43 characters, and result
descriptions were cut off at ~53 characters.  And result descriptions
included headings, further reducing relevant visible text.  For example,
the visible description for `ActionCable::Connection::Base`, which has
the heading "Action Cable Connection Base", was "Action Cable Connection
Base For every WebSocket".  Result descriptions also included code
blocks which were then mangled by `Searchdoc.Panel`'s `stripHTML`
function.  For example, the description for `ActiveModel::API::new` was

  ```html
  <p>Initializes a new model with the given <code>params</code>.

  <pre><code>class Person
    include ActiveModel::API
    attr_accessor ...
  </code></pre>
  ```

which was transformed to

  ```
  Initializes a new model with the given params.

  <codeclass Person
    include ActiveModel::API
    attr_accessor ...
  </pre
  ```

With this commit, search results now always display the full name.
Result descriptions are also fully displayed, including non-link HTML,
and are now comprised of (up to) the first 130 characters of the leading
paragraph of the RDoc comment.  For example, the description of
`ActiveModel::API::new` becomes "Initializes a new model with the given
<code>params</code>."

[`searcher.js`]: https://github.com/ruby/rdoc/blob/v6.5.0/lib/rdoc/generator/template/json_index/js/searcher.js
  • Loading branch information
jonathanhefner committed Sep 9, 2023
1 parent cc68136 commit d00ca33
Show file tree
Hide file tree
Showing 12 changed files with 871 additions and 368 deletions.
4 changes: 1 addition & 3 deletions lib/rdoc/generator/template/rails/_head.rhtml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@
<link rel="stylesheet" href="/css/highlight.css" type="text/css" media="screen" />

<script src="/js/jquery-3.5.1.min.js" type="text/javascript"></script>
<script src="/js/main.js" type="text/javascript"></script>
<script src="/js/main.js" type="module"></script>
<script src="/js/@hotwired--turbo.js" type="module"></script>
<script src="/js/search_index.js" type="text/javascript"></script>
<script src="/js/searcher.js" type="text/javascript"></script>
<script src="/panel/tree.js" type="text/javascript"></script>
<script src="/js/searchdoc.js" type="text/javascript"></script>
<meta name="data-tree-keys" content='<%= tree_keys %>'>
Expand Down
9 changes: 4 additions & 5 deletions lib/rdoc/generator/template/rails/_panel.rhtml
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,13 @@
<input id="panel__state" type="checkbox">

<div id="panel__tray" class="panel__tray" data-turbo-permanent>
<input id="search" type="text" autocomplete="off" tabindex="-1" autosave="searchdoc" results="10"
<input id="search" class="panel__search" type="text"
tabindex="-1" autocomplete="off" autosave="searchdoc"
placeholder="Search (/) for a class, method, ...">

<div id="results" class="panel__results"></div>

<div class="panel__tree">
<div class="result hidden">
<ul>
</ul>
</div>
<div class="tree">
<ul>
</ul>
Expand Down
160 changes: 123 additions & 37 deletions lib/rdoc/generator/template/rails/resources/css/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ a {
}
}

a:has(> code:only-child) {
a:has(> code):not(:has(> :not(code))) {
text-decoration: none;
}

Expand All @@ -67,10 +67,6 @@ a code {
background-size: 1.1em;
}

.hidden {
display: none;
}

.kind {
font-family: monospace;
font-weight: bold;
Expand Down Expand Up @@ -199,7 +195,9 @@ a.back-to-top.show {
*/

html {
--panel-width: 100dvw;
--banner-height: 3.5rem;
--search-height: calc(0.70 * var(--banner-height));
scroll-padding-top: calc(var(--banner-height) + 1rem);
}

Expand All @@ -208,14 +206,18 @@ html {
top: 0;
left: 0;
z-index: 90;
width: 100dvw;
width: var(--panel-width);
}

#panel__state {
display: none;
}


/*
* Navigation panel - Banner
*/

.banner {
height: var(--banner-height);
background-color: var(--brand-color);
Expand Down Expand Up @@ -257,24 +259,25 @@ html {
}


/*
* Navigation panel - Tray
*/

.panel__tray {
background: var(--body-bg);

position: fixed;
top: var(--banner-height);
width: inherit;
position: absolute;

overflow-y: hidden;
transition: max-height 0.2s ease-in-out;
max-height: 0;
transition: height 0.2s ease-in-out;
height: 0;
}

#panel__state:checked ~ .panel__tray {
max-height: 100dvh;
height: 100dvh;
}

#search {
height: calc(0.70 * var(--banner-height));
.panel__search {
height: var(--search-height);
width: 100%;

background: url('../i/search.svg') no-repeat;
Expand All @@ -284,51 +287,133 @@ html {
color: inherit;

border: 0;
box-shadow: -1px 1px 3px rgba(0, 0, 0, 0.2);
box-shadow: 1px 1px 4px color-mix(in srgb, currentColor 50%, transparent) inset;
}

.panel__tree {
height: calc(100dvh - 1.70 * var(--banner-height));
overflow-x: hidden;
.panel__results, .panel__tree {
position: relative;
height: calc(100dvh - var(--banner-height) - var(--search-height));
width: var(--panel-width);
transition: opacity 0.15s ease-in-out;

overflow-y: auto;
overscroll-behavior: contain;
}

/* Always contain scroll events in .panel__tree (on mobile) */
.panel__tree .tree {
height: calc(100% + 1px);
/* Force scroll to always contain scroll events (on mobile) */
:is(.panel__results, .panel__tree)::after {
content: "";
height: 1px;
width: 1px;
position: absolute;
bottom: -1px;
z-index: -1;
}

.panel__results:not(.active),
.panel__search:placeholder-shown ~ .panel__results,
.panel__search:not(:placeholder-shown) ~ .panel__results.active ~ .panel__tree {
/* `display: none` disables animations, so simulate it instead */
max-height: 0;
max-width: 0;
padding: 0;

opacity: 0;
}

.panel__tree .result:not(.hidden) ~ .tree {
display: none;
.panel__tree {
overflow-x: hidden;
}


@media (min-width: 600px) {
html {
scroll-padding-top: 1rem;
/*
* Navigation panel - Search results
*/

.panel__results {
font-size: 0.95em;
padding: var(--space) var(--space-sm);
scroll-padding: var(--space) 0;
}

.panel__results:empty::before {
content: "No results.";
font-style: italic;
}

.results__result:not(:first-child) {
margin-top: var(--space-lg);
}

.results__result {
border-left: 5px solid color-mix(in srgb, currentColor 10%, transparent);
border-radius: 5px;
padding-left: var(--space-sm);
}

/* Assume physical keyboard exists when not `pointer: coarse` */
@media not (pointer: coarse) {
.results__result.cursor {
border-color: var(--link-color);
}
}

#panel {
height: 0;
width: 300px;
@media (hover: hover) {
.results__result:has(.result__link:hover) {
border-color: var(--link-hover-color);
}
}

#content {
margin-left: 300px;
.result__link {
display: flex;
flex-direction: column;
}

.result__method {
display: flex;
}

.result__method::before {
content: "\221F";
font-size: 1.25em;
margin: -0.3em 0.2em;
color: var(--text-color);
}

.result__summary {
margin: var(--space-sm) 0 0 0;
}

:is(.result__method, .result__summary):empty {
display: none;
}


/*
* Navigation panel on desktop
*/

@media (min-width: 600px) {
html {
--panel-width: 300px;
scroll-padding-top: 1rem;
}

.banner__segment:first-child {
display: none;
}

.panel__tray {
min-height: 100dvh;
height: unset;
border-right: 1px solid color-mix(in srgb, currentColor 25%, transparent);
}

.panel__tree .tree {
height: unset;
.panel__results.active {
width: 60ch;
}

:is(.panel__results, .panel__tree)::after {
display: none;
}
}

Expand All @@ -343,6 +428,8 @@ html {

@media (min-width: 600px) {
#content {
margin-top: calc(-1 * var(--banner-height));
margin-left: var(--panel-width);
padding: var(--space-lg) var(--space-xl);
}
}
Expand Down Expand Up @@ -555,8 +642,7 @@ html {
font-style: italic;
}

.description pre,
.description p code {
.description :is(code, pre) {
background: var(--code-bg);
}

Expand Down
Loading

0 comments on commit d00ca33

Please sign in to comment.