Skip to content

Commit 2e8436e

Browse files
committed
feat(search): directly search processing reference
1 parent b334e74 commit 2e8436e

File tree

7 files changed

+55
-50
lines changed

7 files changed

+55
-50
lines changed

scripts/fetchDocs.mjs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ const escapeHTML = (html) =>
115115
* Gets the documentation for a single link
116116
*
117117
* @param {string} link - Link to get doc from
118-
* @returns {Promise<import("./src/documentation").DocumentationVariable>}
118+
* @returns {Promise<import("../src/types").DocumentationVariable>}
119119
*/
120120
const documentVariable = async (link) => {
121121
const documentation = {
@@ -177,7 +177,7 @@ const documentVariable = async (link) => {
177177
* Gets the documentation for a single link
178178
*
179179
* @param {string} link - Link to get doc from
180-
* @returns {Promise<import("./src/documentation").DocumentationFunction>}
180+
* @returns {Promise<import("../src/types").DocumentationFunction>}
181181
*/
182182
const documentFunction = async (link) => {
183183
const documentation = {
@@ -246,7 +246,7 @@ const documentFunction = async (link) => {
246246
* Gets the documentation for a single link
247247
*
248248
* @param {string} link - Link to get doc from
249-
* @returns {Promise<import("./src/documentation").DocumentationClass>}
249+
* @returns {Promise<import("../src/types").DocumentationClass>}
250250
*/
251251
const documentClass = async (link) => {
252252
const documentation = {
@@ -328,7 +328,7 @@ const documentLinks = async ({classLinks, functionLinks, variableLinks}) => {
328328
/**
329329
* All documentation (final object dumped to JSON)
330330
*
331-
* @type {import("./src/documentation").Documentation}
331+
* @type {import("../src/types").Documentation}
332332
*/
333333
const documentation = {}
334334

src/commands/search.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* @copyright (C) 2016 - 2020 Tobiah Zarlez, 2021 Luke Zhang
55
*/
66

7-
import * as search from "../search"
7+
import {search} from "../utils"
88
import vscode from "vscode"
99

1010
const openDocErrorMessage = async (str: string) => {

src/documentation.ts

Lines changed: 8 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44
* @copyright (C) 2021 Luke Zhang
55
*/
66

7+
import type {
8+
Documentation,
9+
DocumentationClass,
10+
DocumentationFunction,
11+
DocumentationVariable,
12+
} from "./types"
713
import documentation from "./documentation-data.yml"
814
import vscode from "vscode"
915

@@ -22,7 +28,7 @@ const getHoveredItem = (line: string, position: number): string | undefined => {
2228
let index = position
2329

2430
for (; index >= 0 && index < line.length; index--) {
25-
if (!/[a-z]|[0-9]|_/iu.test(line[index])) {
31+
if (!/[a-z]|[0-9]|_/iu.test(line[index]!)) {
2632
break
2733
}
2834
}
@@ -34,7 +40,7 @@ const getHoveredItem = (line: string, position: number): string | undefined => {
3440
let index = position
3541

3642
for (; index >= 0 && index < line.length; index++) {
37-
if (!/[a-z]|[0-9]|_/iu.test(line[index])) {
43+
if (!/[a-z]|[0-9]|_/iu.test(line[index]!)) {
3844
break
3945
}
4046
}
@@ -45,35 +51,6 @@ const getHoveredItem = (line: string, position: number): string | undefined => {
4551
return line.slice(itemStart, itemEnd)
4652
}
4753

48-
interface DocumentationVariable {
49-
description: string
50-
examples?: string
51-
docUrl: string
52-
type: "var" | "const"
53-
}
54-
55-
interface DocumentationClass {
56-
description: string
57-
syntax: string
58-
parameters: {[key: string]: string}
59-
docUrl: string
60-
type: "class"
61-
}
62-
63-
interface DocumentationFunction {
64-
description: string
65-
syntax: string
66-
parameters: {[key: string]: string}
67-
returns: string
68-
docUrl: string
69-
type: "function"
70-
}
71-
72-
/* prettier-ignore */
73-
export type Documentation = {
74-
[key: string]: DocumentationFunction | DocumentationVariable | DocumentationClass
75-
};
76-
7754
const documentVariable = (
7855
info: DocumentationVariable,
7956
item: keyof typeof documentation,

src/env.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
declare module "*.yml" {
2-
declare const content: {[key: string]: unknown}
2+
const content: {[key: string]: unknown}
33

44
export default content
55
}

src/types.d.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
export interface DocumentationClass {
2+
description: string
3+
syntax: string
4+
parameters: {[key: string]: string}
5+
docUrl: string
6+
type: "class"
7+
}
8+
9+
export interface DocumentationFunction {
10+
description: string
11+
syntax: string
12+
parameters: {[key: string]: string}
13+
returns: string
14+
docUrl: string
15+
type: "function"
16+
}
17+
18+
export interface DocumentationVariable {
19+
description: string
20+
examples?: string
21+
docUrl: string
22+
type: "var" | "const"
23+
}
24+
25+
export type Documentation = {
26+
[key: string]: DocumentationFunction | DocumentationVariable | DocumentationClass
27+
}

src/utils.ts renamed to src/utils/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,7 @@
44
* @copyright (C) 2021 Luke Zhang
55
*/
66

7+
export * as search from "./search"
8+
79
export const isValidProcessingProject = (path?: string): boolean =>
810
path !== undefined && /^[/_$a-z][/\w$]*$/iu.test(path)

src/search.ts renamed to src/utils/search.ts

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
* @copyright (C) 2016 - 2020 Tobiah Zarlez, 2021 Luke Zhang
55
*/
66

7-
import {searchConfig} from "./config"
7+
import type {Documentation} from "../types"
8+
import documentation from "../documentation-data.yml"
9+
import {searchConfig} from "../config"
810
import vscode from "vscode"
911

1012
const enum Urls {
@@ -16,15 +18,21 @@ const enum Urls {
1618
P5jsSearchDuckDuckGo = "https://duckduckgo.com/?q=!p5+",
1719
}
1820

19-
export const openURL = async (searchBase?: string, url?: string) => {
21+
export const openURL = async (searchBase?: string, url?: string): Promise<void> => {
2022
if (searchBase === "open") {
2123
await vscode.env.openExternal(vscode.Uri.parse(url as string))
2224
} else {
2325
const {processingDocs, searchEngine} = searchConfig
2426
const searchUrl = ((): string => {
27+
let docUrl: string | undefined
28+
2529
if (searchBase === "docs") {
2630
if (!url) {
2731
return processingDocs === "p5js.org" ? Urls.P5jsDocs : Urls.ProcessingorgDocs
32+
} else if (
33+
(docUrl = (documentation as Documentation)[url]?.docUrl) !== undefined
34+
) {
35+
return docUrl
2836
} else if (searchEngine === "DuckDuckGo") {
2937
return processingDocs === "p5js.org"
3038
? `${Urls.P5jsSearchDuckDuckGo}${url}`
@@ -42,7 +50,7 @@ export const openURL = async (searchBase?: string, url?: string) => {
4250
await vscode.env.openExternal(vscode.Uri.parse(searchUrl))
4351
}
4452

45-
return true
53+
return
4654
}
4755

4856
// Slice and Trim
@@ -53,17 +61,8 @@ export const prepareInput = (input: string, start: number, end: number) => {
5361
return ""
5462
}
5563

56-
// Slice to the selection
57-
input = input.slice(start, end)
58-
59-
// Trim white space
60-
input = input.trim()
61-
62-
// Possible future addition:
63-
// Check right here if valid variable/function name to search?
64-
6564
// Everything looks good by this point, so time to open a web browser!
66-
return input
65+
return input.slice(start, end).trim()
6766
}
6867

6968
export const openProcessingDocs = (input: string, start: number, end: number) => {

0 commit comments

Comments
 (0)