-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extension.js
187 lines (170 loc) · 5.54 KB
/
extension.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
const vscode = require("vscode");
const axios = require("axios");
async function getSOSearch(term, type) {
if (type == "title") {
return await axios.get(
`https://api.stackexchange.com/2.3/search/advanced?order=asc&sort=votes&title=${term}&site=stackoverflow`
);
}
if (type == "query") {
return await axios.get(
`https://api.stackexchange.com/2.3/search/advanced?order=desc&sort=votes&q=${term}&site=stackoverflow`
);
}
}
function getQuestionsFromResponse(response) {
var questions = response.data.items.map((question) => {
return {
label: question.title,
detail: `By ${question.owner.display_name} | Answers: ${question.answer_count} | Views: ${question.view_count}`,
link: question.link,
};
});
return questions;
}
async function activate(context) {
let soSearchDisposable = vscode.commands.registerCommand(
"stack-search.getSearchResultsFromSO",
async function () {
vscode.window.showInformationMessage("Search the Question");
var inputQuestion = await vscode.window.showInputBox({
placeHolder: "Type your question here",
});
if (inputQuestion == "" || inputQuestion == null) {
vscode.window.showInformationMessage("No Search Terms Provided");
return;
}
var soResponse;
var questions;
await vscode.window.withProgress(
{
location: vscode.ProgressLocation.Notification,
title: "Fetching Questions",
},
async (_) => {
console.log(_);
soResponse = await getSOSearch(inputQuestion, "title");
questions = getQuestionsFromResponse(soResponse);
if (questions.length === 0) {
soResponse = await getSOSearch(inputQuestion, "query");
questions = getQuestionsFromResponse(soResponse);
}
}
);
if (questions.length === 0) {
vscode.window.showInformationMessage(
"No questions found for your search"
);
return;
}
var question = await vscode.window.showQuickPick(questions, {
matchOnDetail: true,
});
if (question == null) {
vscode.window.showInformationMessage("No Question Selected");
return;
}
vscode.env.openExternal(question.link);
}
);
let soQuestionsDisposable = vscode.commands.registerCommand(
"stack-search.getQuestionsFromSO",
async function () {
var inputTag = await vscode.window.showInputBox({
placeHolder: "Write comma-separated tags",
});
if (inputTag == "" || inputTag == null) {
vscode.window.showInformationMessage("No tags provided");
return;
}
const tags = inputTag.split(",");
var soResponse;
await vscode.window.withProgress(
{
location: vscode.ProgressLocation.Notification,
title: "Fetching Questions",
},
async (_) => {
console.log(_);
soResponse = await axios.get(
`https://api.stackexchange.com/2.3/questions?order=desc&sort=activity&tagged=${tags
.map((tagStr) => tagStr.trim())
.join(";")}&site=stackoverflow`
);
}
);
var questions = soResponse.data.items.map((question) => {
return {
label: question.title,
detail: `By ${question.owner.display_name} | Answers: ${question.answer_count} | Views: ${question.view_count}`,
link: question.link,
};
});
if (questions.length === 0) {
vscode.window.showInformationMessage("No Questions Found for the Tags");
return;
}
var question = await vscode.window.showQuickPick(questions, {
matchOnDetail: true,
});
if (question == null) {
vscode.window.showInformationMessage("No Question Selected");
return;
}
vscode.env.openExternal(question.link);
}
);
let soFavQuestionsDisposable = vscode.commands.registerCommand(
"stack-search.getFavQuestionsFromSO",
() => favQuestionHandler()
);
context.subscriptions.push(soSearchDisposable);
context.subscriptions.push(soQuestionsDisposable);
context.subscriptions.push(soFavQuestionsDisposable);
}
async function favQuestionHandler() {
var favoriteTags = vscode.workspace
.getConfiguration()
.get("stackSearch.config.favoriteTags");
const tags = favoriteTags.split(",");
var soResponse;
await vscode.window.withProgress(
{
location: vscode.ProgressLocation.Notification,
title: `Fetching Questions (Tags: ${favoriteTags})`,
},
async (_) => {
console.log(_);
soResponse = await axios.get(
`https://api.stackexchange.com/2.3/questions?order=desc&sort=activity&tagged=${tags
.map((tagStr) => tagStr.trim())
.join(";")}&site=stackoverflow`
);
}
);
var questions = soResponse.data.items.map((question) => {
return {
label: question.title,
detail: `By ${question.owner.display_name} | Answers: ${question.answer_count} | Views: ${question.view_count}`,
link: question.link,
};
});
if (questions.length === 0) {
vscode.window.showInformationMessage("No questions found for the tags");
return;
}
var question = await vscode.window.showQuickPick(questions, {
matchOnDetail: true,
});
if (question == null) {
vscode.window.showInformationMessage("No question selected");
return;
}
vscode.env.openExternal(question.link);
}
// this method is called when your extension is deactivated
function deactivate() {}
module.exports = {
activate,
deactivate,
};