Skip to content

Commit

Permalink
Rollup merge of #118452 - notriddle:coloncolonspace, r=GuillaumeGomez…
Browse files Browse the repository at this point in the history
…,jsha

rustdoc-search: allow spaces around `::` in path query

This restriction made sense back when spaces separated function parameters, but now that they separate path components, there's no real ambiguity any more.

Additionally, the Rust language allows it.

The other two commits are misc code cleanup.
  • Loading branch information
matthiaskrgr authored Nov 30, 2023
2 parents e55544c + 930cba8 commit e9a870d
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 51 deletions.
31 changes: 11 additions & 20 deletions src/librustdoc/html/static/js/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -287,10 +287,6 @@ function initSearch(rawSearchIndex) {
}
}

function isWhitespace(c) {
return " \t\n\r".indexOf(c) !== -1;
}

function isSpecialStartCharacter(c) {
return "<\"".indexOf(c) !== -1;
}
Expand Down Expand Up @@ -408,7 +404,7 @@ function initSearch(rawSearchIndex) {
* @return {boolean}
*/
function isPathSeparator(c) {
return c === ":" || isWhitespace(c);
return c === ":" || c === " ";
}

/**
Expand All @@ -425,7 +421,7 @@ function initSearch(rawSearchIndex) {
const c = parserState.userQuery[pos - 1];
if (c === lookingFor) {
return true;
} else if (!isWhitespace(c)) {
} else if (c !== " ") {
break;
}
pos -= 1;
Expand Down Expand Up @@ -454,7 +450,7 @@ function initSearch(rawSearchIndex) {
function skipWhitespace(parserState) {
while (parserState.pos < parserState.userQuery.length) {
const c = parserState.userQuery[parserState.pos];
if (!isWhitespace(c)) {
if (c !== " ") {
break;
}
parserState.pos += 1;
Expand All @@ -473,8 +469,6 @@ function initSearch(rawSearchIndex) {
const path = name.trim();
if (path.length === 0 && generics.length === 0) {
throw ["Unexpected ", parserState.userQuery[parserState.pos]];
} else if (path === "*") {
throw ["Unexpected ", "*"];
}
if (query.literalSearch && parserState.totalElems - parserState.genericsElems > 0) {
throw ["Cannot have more than one element if you use quotes"];
Expand Down Expand Up @@ -512,18 +506,15 @@ function initSearch(rawSearchIndex) {
bindingName,
};
}
const quadcolon = /::\s*::/.exec(path);
if (path.startsWith("::")) {
throw ["Paths cannot start with ", "::"];
} else if (path.endsWith("::")) {
throw ["Paths cannot end with ", "::"];
} else if (path.includes("::::")) {
throw ["Unexpected ", "::::"];
} else if (path.includes(" ::")) {
throw ["Unexpected ", " ::"];
} else if (path.includes(":: ")) {
throw ["Unexpected ", ":: "];
}
const pathSegments = path.split(/::|\s+/);
} else if (quadcolon !== null) {
throw ["Unexpected ", quadcolon[0]];
}
const pathSegments = path.split(/(?:::\s*)|(?:\s+(?:::\s*)?)/);
// In case we only have something like `<p>`, there is no name.
if (pathSegments.length === 0 || (pathSegments.length === 1 && pathSegments[0] === "")) {
if (generics.length > 0 || prevIs(parserState, ">")) {
Expand Down Expand Up @@ -604,7 +595,7 @@ function initSearch(rawSearchIndex) {
} else {
while (parserState.pos + 1 < parserState.length) {
const next_c = parserState.userQuery[parserState.pos + 1];
if (!isWhitespace(next_c)) {
if (next_c !== " ") {
break;
}
parserState.pos += 1;
Expand Down Expand Up @@ -958,7 +949,7 @@ function initSearch(rawSearchIndex) {
query.literalSearch = false;
foundStopChar = true;
continue;
} else if (isWhitespace(c)) {
} else if (c === " ") {
skipWhitespace(parserState);
continue;
}
Expand Down Expand Up @@ -1118,7 +1109,7 @@ function initSearch(rawSearchIndex) {
}
}
}
userQuery = userQuery.trim();
userQuery = userQuery.trim().replace(/\r|\n|\t/g, " ");
const parserState = {
length: userQuery.length,
pos: 0,
Expand Down
45 changes: 27 additions & 18 deletions tests/rustdoc-js-std/parser-errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ const PARSED = [
userQuery: "-> <p>",
error: "Found generics without a path",
},
{
query: '-> *',
elems: [],
foundElems: 0,
original: "-> *",
returned: [],
userQuery: "-> *",
error: "Unexpected `*`",
},
{
query: 'a<"P">',
elems: [],
Expand Down Expand Up @@ -143,6 +152,24 @@ const PARSED = [
userQuery: "a::::b",
error: "Unexpected `::::`",
},
{
query: "a:: ::b",
elems: [],
foundElems: 0,
original: "a:: ::b",
returned: [],
userQuery: "a:: ::b",
error: "Unexpected `:: ::`",
},
{
query: "a::\t::b",
elems: [],
foundElems: 0,
original: "a:: ::b",
returned: [],
userQuery: "a:: ::b",
error: "Unexpected `:: ::`",
},
{
query: "a::b::",
elems: [],
Expand Down Expand Up @@ -314,24 +341,6 @@ const PARSED = [
userQuery: 'a<->',
error: 'Unexpected `-` after `<`',
},
{
query: "a:: a",
elems: [],
foundElems: 0,
original: 'a:: a',
returned: [],
userQuery: 'a:: a',
error: 'Unexpected `:: `',
},
{
query: "a ::a",
elems: [],
foundElems: 0,
original: 'a ::a',
returned: [],
userQuery: 'a ::a',
error: 'Unexpected ` ::`',
},
{
query: "a<a>:",
elems: [],
Expand Down
48 changes: 48 additions & 0 deletions tests/rustdoc-js-std/parser-paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,54 @@ const PARSED = [
userQuery: "a::b",
error: null,
},
{
query: "a:: a",
elems: [{
name: "a:: a",
fullPath: ["a", "a"],
pathWithoutLast: ["a"],
pathLast: "a",
generics: [],
typeFilter: -1,
}],
foundElems: 1,
original: 'a:: a',
returned: [],
userQuery: 'a:: a',
error: null,
},
{
query: "a ::a",
elems: [{
name: "a ::a",
fullPath: ["a", "a"],
pathWithoutLast: ["a"],
pathLast: "a",
generics: [],
typeFilter: -1,
}],
foundElems: 1,
original: 'a ::a',
returned: [],
userQuery: 'a ::a',
error: null,
},
{
query: "a :: a",
elems: [{
name: "a :: a",
fullPath: ["a", "a"],
pathWithoutLast: ["a"],
pathLast: "a",
generics: [],
typeFilter: -1,
}],
foundElems: 1,
original: 'a :: a',
returned: [],
userQuery: 'a :: a',
error: null,
},
{
query: 'A::B,C',
elems: [
Expand Down
22 changes: 11 additions & 11 deletions tests/rustdoc-js-std/parser-separators.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const PARSED = [
query: 'aaaaaa b',
elems: [
{
name: 'aaaaaa\tb',
name: 'aaaaaa b',
fullPath: ['aaaaaa', 'b'],
pathWithoutLast: ['aaaaaa'],
pathLast: 'b',
Expand All @@ -14,9 +14,9 @@ const PARSED = [
},
],
foundElems: 1,
original: "aaaaaa b",
original: "aaaaaa b",
returned: [],
userQuery: "aaaaaa b",
userQuery: "aaaaaa b",
error: null,
},
{
Expand All @@ -40,9 +40,9 @@ const PARSED = [
},
],
foundElems: 2,
original: "aaaaaa, b",
original: "aaaaaa, b",
returned: [],
userQuery: "aaaaaa, b",
userQuery: "aaaaaa, b",
error: null,
},
{
Expand Down Expand Up @@ -93,7 +93,7 @@ const PARSED = [
query: 'a\tb',
elems: [
{
name: 'a\tb',
name: 'a b',
fullPath: ['a', 'b'],
pathWithoutLast: ['a'],
pathLast: 'b',
Expand All @@ -102,9 +102,9 @@ const PARSED = [
},
],
foundElems: 1,
original: "a\tb",
original: "a b",
returned: [],
userQuery: "a\tb",
userQuery: "a b",
error: null,
},
{
Expand Down Expand Up @@ -176,7 +176,7 @@ const PARSED = [
pathLast: 'a',
generics: [
{
name: 'b\tc',
name: 'b c',
fullPath: ['b', 'c'],
pathWithoutLast: ['b'],
pathLast: 'c',
Expand All @@ -187,9 +187,9 @@ const PARSED = [
},
],
foundElems: 1,
original: "a<b\tc>",
original: "a<b c>",
returned: [],
userQuery: "a<b\tc>",
userQuery: "a<b c>",
error: null,
},
];
4 changes: 2 additions & 2 deletions tests/rustdoc-js-std/parser-weird-queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ const PARSED = [
query: 'mod\t:',
elems: [],
foundElems: 0,
original: 'mod\t:',
original: 'mod :',
returned: [],
userQuery: 'mod\t:',
userQuery: 'mod :',
error: "Unexpected `:` (expected path after type filter `mod:`)",
},
];

0 comments on commit e9a870d

Please sign in to comment.