Skip to content

Commit 0e2feb7

Browse files
committed
feat(search): add py.processing.org search
1 parent 2e8436e commit 0e2feb7

File tree

4 files changed

+117
-50
lines changed

4 files changed

+117
-50
lines changed

package.json

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,12 @@
6666
{
6767
"command": "processing.OpenDocs",
6868
"title": "Processing: Open Documentation for Selection",
69-
"enablement": "resourceLangId == pde"
69+
"enablement": "resourceLangId == pde || resourceLangId == python && config.processing.py.isEnabled"
7070
},
7171
{
7272
"command": "processing.SearchWebsite",
7373
"title": "Processing: Search Processing Website",
74-
"enablement": "resourceLangId == pde"
74+
"enablement": "resourceLangId == pde || resourceLangId == python && config.processing.py.isEnabled"
7575
}
7676
],
7777
"languages": [
@@ -142,16 +142,20 @@
142142
},
143143
"processing.docs": {
144144
"type": "string",
145-
"default": "processing.org",
145+
"default": "auto",
146146
"enum": [
147147
"processing.org",
148-
"p5js.org"
148+
"p5js.org",
149+
"py.processing.org",
150+
"auto"
149151
],
150152
"enumDescriptions": [
151153
"Use processing.org for documentation",
152-
"Use p5js for documentation"
154+
"Use p5js for documentation",
155+
"Use py.processing.org for documentation",
156+
"Automatically determine either processing.org or py.processing.org (never p5js) depending on the current file"
153157
],
154-
"description": "Which documentation should this extension use?"
158+
"description": "Which documentation this extension should use"
155159
},
156160
"processing.search": {
157161
"type": "string",

rollup.config.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,21 @@ import {terser} from "rollup-plugin-terser"
55
import typescript from "@rollup/plugin-typescript"
66

77
const banner = `/**
8-
* processing-vscode - Processing Language Support for VSCode
8+
* Processing-vscode - Processing Language Support for VSCode
9+
* https://github.com/Luke-zhang-04/processing-vscode
10+
*
11+
* @license MIT
912
* @version 2.2.0
10-
* @copyright (C) 2016 - 2020 Tobiah Zarlez, 2021 Luke Zhang
1113
* @preserve
14+
* @copyright (C) 2016 - 2020 Tobiah Zarlez, 2021 Luke Zhang
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
1223
*/
1324
`
1425

src/config.ts

Lines changed: 42 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ const getProcessingCommand = (): string => {
1212
.get<unknown>("processing.processingPath", "processing-java")
1313

1414
if (typeof config !== "string") {
15-
vscode.window.showErrorMessage(
16-
"Config option processing.processingPath must be of type string",
17-
)
15+
const msg = "Config option processing.processingPath must be of type string"
1816

19-
throw new Error("Config option processing.processingPath must be of type string")
17+
vscode.window.showErrorMessage(msg)
18+
19+
throw new Error(msg)
2020
}
2121

2222
return config
@@ -28,11 +28,11 @@ const getJavaCommand = (): string => {
2828
.get<unknown>("processing.py.javaPath", "java")
2929

3030
if (typeof config !== "string") {
31-
vscode.window.showErrorMessage(
32-
"Config option processing.py.javaPath must be of type string",
33-
)
31+
const msg = "Config option processing.py.javaPath must be of type string"
32+
33+
vscode.window.showErrorMessage(msg)
3434

35-
throw new Error("Config option processing.py.javaPath must be of type string")
35+
throw new Error(msg)
3636
}
3737

3838
return config
@@ -44,11 +44,11 @@ const getJarPath = (): string => {
4444
.get<unknown>("processing.py.jarPath", "processing-py.jar")
4545

4646
if (typeof config !== "string") {
47-
vscode.window.showErrorMessage(
48-
"Config option processing.py.jarPath must be of type string",
49-
)
47+
const msg = "Config option processing.py.jarPath must be of type string"
48+
49+
vscode.window.showErrorMessage(msg)
5050

51-
throw new Error("Config option processing.py.jarPath must be of type string")
51+
throw new Error(msg)
5252
}
5353

5454
return config
@@ -60,27 +60,37 @@ const getPythonEnablement = (): boolean => {
6060
.get<boolean>("processing.py.isEnabled", true)
6161

6262
if (typeof isEnabled !== "boolean") {
63-
vscode.window.showErrorMessage("Config option processing.py.isEnabled should be a boolean")
63+
const msg = "Config option processing.py.isEnabled should be a boolean"
6464

65-
throw new Error("Config option processing.py.isEnabled should be a boolean")
65+
vscode.window.showErrorMessage(msg)
66+
67+
throw new Error(msg)
6668
}
6769

6870
return isEnabled
6971
}
7072

71-
const getSearchConfig = (): {searchEngine: string; processingDocs: string} => {
73+
type DocOptions = "processing.org" | "p5js.org" | "py.processing.org" | "auto"
74+
type SearchEngines = "Google" | "DuckDuckGo"
75+
76+
const getSearchConfig = (): {searchEngine: SearchEngines; processingDocs: DocOptions} => {
7277
const config = vscode.workspace.getConfiguration("processing")
73-
const processingDocs = config.get<unknown>("docs", "processing.org")
74-
const searchEngine = config.get<unknown>("search", "Google")
78+
const processingDocs = config.get<DocOptions>("docs", "auto")
79+
const searchEngine = config.get<SearchEngines>("search", "Google")
7580

76-
if (typeof processingDocs !== "string" || typeof searchEngine !== "string") {
77-
vscode.window.showErrorMessage(
78-
"Config options processing.processingDocs and processing.searchEngine must be of type string",
79-
)
81+
if (!["processing.org", "p5js.org", "py.processing.org", "auto"].includes(processingDocs)) {
82+
const msg =
83+
'Config option processing.docs must be "processing.org" | "p5js.org" | "py.processing.org" | "auto"'
8084

81-
throw new Error(
82-
"Config options processing.processingDocs and processing.searchEngine must be of type string",
83-
)
85+
vscode.window.showErrorMessage(msg)
86+
87+
throw new Error(msg)
88+
} else if (!["Google", "DuckDuckGo"].includes(searchEngine)) {
89+
const msg = 'Config option processing.search must be "Google" | "DuckDuckGo"'
90+
91+
vscode.window.showErrorMessage(msg)
92+
93+
throw new Error(msg)
8494
}
8595

8696
return {
@@ -95,11 +105,11 @@ const getshouldEnableDiagnostics = (): boolean => {
95105
.get<boolean>("processing.shouldGiveDiagnostics", true)
96106

97107
if (typeof shouldGiveDiagnostics !== "boolean") {
98-
vscode.window.showErrorMessage(
99-
"Config option processing.shouldGiveDiagnostics must be of type string",
100-
)
108+
const msg = "Config option processing.shouldGiveDiagnostics must be of type string"
101109

102-
throw new Error("Config option processing.shouldGiveDiagnostics must be of type string")
110+
vscode.window.showErrorMessage(msg)
111+
112+
throw new Error(msg)
103113
}
104114

105115
return shouldGiveDiagnostics
@@ -111,11 +121,11 @@ const getQuoteEnablement = (): boolean => {
111121
.get<"always" | "auto">("processing.runPathQuotes", "auto")
112122

113123
if (shouldQuotePath !== "always" && shouldQuotePath !== "auto") {
114-
vscode.window.showErrorMessage(
115-
"Config option processing.runPathQuotes should be auto or always",
116-
)
124+
const msg = 'Config option processing.runPathQuotes should be "auto" or "always"'
125+
126+
vscode.window.showErrorMessage(msg)
117127

118-
throw new Error("Config option processing.runPathQuotes should be auto or always")
128+
throw new Error(msg)
119129
}
120130

121131
return shouldQuotePath === "always"

src/utils/search.ts

Lines changed: 52 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ const enum Urls {
1616
P5jsDocs = "https://p5js.org/reference/",
1717
P5jsSearchGoogle = "https://www.google.com/search?as_sitesearch=p5js.org&as_q=",
1818
P5jsSearchDuckDuckGo = "https://duckduckgo.com/?q=!p5+",
19+
PyDocs = "https://py.processing.org/reference/",
20+
PySearchGoogle = "https://www.google.com/search?as_sitesearch=py.processing.org&as_q=",
21+
PySearchDuckDuckGo = "https://duckduckgo.com/?q=%5Csite%3Apy.processing.org+",
1922
}
2023

2124
export const openURL = async (searchBase?: string, url?: string): Promise<void> => {
@@ -24,24 +27,63 @@ export const openURL = async (searchBase?: string, url?: string): Promise<void>
2427
} else {
2528
const {processingDocs, searchEngine} = searchConfig
2629
const searchUrl = ((): string => {
27-
let docUrl: string | undefined
30+
let directDocUrl: string | undefined
2831

2932
if (searchBase === "docs") {
33+
const {
34+
window: {activeTextEditor},
35+
} = vscode
36+
const languageId = activeTextEditor?.document.languageId
37+
3038
if (!url) {
31-
return processingDocs === "p5js.org" ? Urls.P5jsDocs : Urls.ProcessingorgDocs
39+
if (processingDocs === "p5js.org") {
40+
return Urls.P5jsDocs
41+
} else if (
42+
(processingDocs === "auto" && languageId === "pyton") ||
43+
processingDocs === "py.processing.org"
44+
) {
45+
return Urls.PyDocs
46+
}
47+
48+
// (processingDocs === "auto" && languageId === "pde")
49+
// || processingDocs === "processing.org"
50+
return Urls.ProcessingorgDocs
3251
} else if (
33-
(docUrl = (documentation as Documentation)[url]?.docUrl) !== undefined
52+
// Look for entry directly in documentation data
53+
(processingDocs === "auto" || processingDocs === "processing.org") &&
54+
languageId === "pde" &&
55+
(directDocUrl = (documentation as Documentation)[url]?.docUrl) !== undefined
3456
) {
35-
return docUrl
57+
return directDocUrl
3658
} else if (searchEngine === "DuckDuckGo") {
37-
return processingDocs === "p5js.org"
38-
? `${Urls.P5jsSearchDuckDuckGo}${url}`
39-
: `${Urls.ProcessingorgSearchDuckDuckGo}${url}`
59+
// Search Engine == "google"
60+
if (processingDocs === "p5js.org") {
61+
return `${Urls.P5jsSearchDuckDuckGo}${url}`
62+
} else if (
63+
(processingDocs === "auto" && languageId === "pyton") ||
64+
processingDocs === "py.processing.org"
65+
) {
66+
return `${Urls.PySearchDuckDuckGo}${url}`
67+
}
68+
69+
// (processingDocs === "auto" && languageId === "pde")
70+
// || processingDocs === "processing.org"
71+
return `${Urls.ProcessingorgSearchDuckDuckGo}${url}`
72+
}
73+
74+
// Search Engine == "google"
75+
if (processingDocs === "p5js.org") {
76+
return `${Urls.P5jsSearchGoogle}${url}`
77+
} else if (
78+
(processingDocs === "auto" && languageId === "pyton") ||
79+
processingDocs === "py.processing.org"
80+
) {
81+
return `${Urls.PySearchGoogle}${url}`
4082
}
4183

42-
return processingDocs === "p5js.org"
43-
? `${Urls.P5jsSearchGoogle}${url}`
44-
: `${Urls.ProcessingorgSearchGoogle}${url}`
84+
// (processingDocs === "auto" && languageId === "pde")
85+
// || processingDocs === "processing.org"
86+
return `${Urls.ProcessingorgSearchGoogle}${url}`
4587
}
4688

4789
return searchBase ?? ""

0 commit comments

Comments
 (0)