quick-open.js (2738B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at <http://mozilla.org/MPL/2.0/>. */ 4 5 import { endTruncateStr } from "./utils"; 6 import { getSourceClassnames, getRelativeUrl } from "./source"; 7 8 export const MODIFIERS = { 9 "@": "functions", 10 "#": "variables", 11 ":": "goto", 12 "?": "shortcuts", 13 }; 14 15 export function parseQuickOpenQuery(query) { 16 const startsWithModifier = 17 query[0] === "@" || 18 query[0] === "#" || 19 query[0] === ":" || 20 query[0] === "?"; 21 22 if (startsWithModifier) { 23 const modifier = query[0]; 24 return MODIFIERS[modifier]; 25 } 26 27 const isGotoSource = query.includes(":", 1); 28 29 if (isGotoSource) { 30 return "gotoSource"; 31 } 32 33 return "sources"; 34 } 35 36 export function parseLineColumn(query) { 37 const [, line, column] = query.split(":"); 38 const lineNumber = parseInt(line, 10); 39 const columnNumber = parseInt(column, 10); 40 if (isNaN(lineNumber)) { 41 return null; 42 } 43 if (isNaN(columnNumber)) { 44 return { line: lineNumber }; 45 } 46 // columnNumber here is the user input value which is 1-based. 47 // Whereas in location objects, line is 1-based, and column is 0-based. 48 return { 49 line: lineNumber, 50 column: columnNumber - 1, 51 }; 52 } 53 54 export function formatSourceForList( 55 source, 56 hasTabOpened, 57 isBlackBoxed, 58 projectDirectoryRoot 59 ) { 60 const prefix = "dbg-img-"; 61 const relativeUrlWithQuery = `${getRelativeUrl( 62 source, 63 projectDirectoryRoot 64 )}${source.displayURL.search || ""}`; 65 const subtitle = endTruncateStr(relativeUrlWithQuery, 100); 66 const value = relativeUrlWithQuery; 67 return { 68 value, 69 title: source.shortName, 70 subtitle, 71 icon: hasTabOpened 72 ? `tab ${prefix}result-item-icon` 73 : `result-item-icon ${prefix}${getSourceClassnames(source, isBlackBoxed)}`, 74 id: source.id, 75 url: source.url, 76 source, 77 }; 78 } 79 80 export function formatSymbol(symbol) { 81 return { 82 id: `${symbol.name}:${symbol.location.start.line}`, 83 title: symbol.name, 84 subtitle: `${symbol.location.start.line}`, 85 value: symbol.name, 86 location: symbol.location, 87 }; 88 } 89 90 export function formatShortcutResults() { 91 return [ 92 { 93 value: L10N.getStr("symbolSearch.search.functionsPlaceholder.title"), 94 title: `@ ${L10N.getStr("symbolSearch.search.functionsPlaceholder")}`, 95 id: "@", 96 }, 97 { 98 value: L10N.getStr("symbolSearch.search.variablesPlaceholder.title"), 99 title: `# ${L10N.getStr("symbolSearch.search.variablesPlaceholder")}`, 100 id: "#", 101 }, 102 { 103 value: L10N.getStr("gotoLineModal.title"), 104 title: `: ${L10N.getStr("gotoLineModal.placeholder")}`, 105 id: ":", 106 }, 107 ]; 108 }