|
1 | | -import * as vscode from "vscode"; |
2 | | -import * as path from "path"; |
3 | | -import { createSnippet } from "./services/codeishotServices"; |
4 | | -import { saveToken, login } from "./login"; |
5 | | - |
6 | | -const UI_BASE_URL: string = process.env.UI_BASE_URL || "https://codeishot.com"; |
7 | | - |
8 | | -interface PostData { |
9 | | - title: string; |
10 | | - code: string; |
11 | | - style: string; |
12 | | - language: string; |
13 | | -} |
14 | | - |
15 | | -interface Snippet { |
16 | | - id: string; |
17 | | - title: string; |
18 | | - language: string; |
19 | | - style: string; |
20 | | - code: string; |
21 | | - createdAt: string; |
22 | | - editedAt: string; |
23 | | -} |
24 | | - |
25 | | -function getSelectedText(editor: vscode.TextEditor): string | null { |
26 | | - const selection = editor.selection; |
27 | | - const text = editor.document.getText(selection); |
28 | | - |
29 | | - if (!text) { |
30 | | - vscode.window.showErrorMessage("No text selected"); |
31 | | - return null; |
32 | | - } |
33 | | - |
34 | | - return text; |
35 | | -} |
36 | | - |
37 | | -function getCurrentFileName(): string | null { |
38 | | - const editor = vscode.window.activeTextEditor; |
39 | | - if (editor) { |
40 | | - const document = editor.document; |
41 | | - const fullPath = document.fileName; |
42 | | - return path.basename(fullPath); |
43 | | - } |
44 | | - return null; |
45 | | -} |
46 | | - |
47 | | -function getLanguageIdentifier(): string | null { |
48 | | - const editor = vscode.window.activeTextEditor; |
49 | | - if (editor) { |
50 | | - const languageId = editor.document.languageId; |
51 | | - return languageId; |
52 | | - } |
53 | | - return null; |
54 | | -} |
55 | | - |
56 | | -function checkCreateSnippetStatusCode(status: number, data: PostData) { |
57 | | - switch (status) { |
58 | | - case 400: |
59 | | - vscode.window.showWarningMessage( |
60 | | - "This language is not available, sending snippet as a Plain Text" |
61 | | - ); |
62 | | - const newRes = postSnippetWithValidatedData(data); |
63 | | - return newRes; |
64 | | - case 429: |
65 | | - vscode.window.showErrorMessage("Too many requests!"); |
66 | | - break; |
67 | | - default: |
68 | | - break; |
69 | | - } |
70 | | -} |
71 | | - |
72 | | -async function postSnippet(data: PostData): Promise<Snippet> { |
73 | | - let res = await createSnippet(data); |
74 | | - if (res.status_code) checkCreateSnippetStatusCode(res.status_code, data); |
75 | | - return res.data as Promise<Snippet>; |
76 | | -} |
77 | | - |
78 | | -async function postSnippetWithValidatedData(data: PostData): Promise<Snippet> { |
79 | | - let plainTextData: PostData = { ...data }; |
80 | | - plainTextData.language = "plaintext"; |
81 | | - |
82 | | - return await createSnippet(plainTextData) |
83 | | - .then((res) => res.data) |
84 | | - .catch(() => { |
85 | | - vscode.window.showWarningMessage("Please try again!"); |
86 | | - }); |
87 | | -} |
88 | | - |
89 | | -async function handlePostResponse(data: Snippet) { |
90 | | - if (data && data.id) { |
91 | | - const snippetUrl = `${UI_BASE_URL}/${data.id}`; |
92 | | - try { |
93 | | - await vscode.env.clipboard.writeText(snippetUrl); |
94 | | - vscode.window.showInformationMessage("URL copied to clipboard"); |
95 | | - } catch (error) { |
96 | | - vscode.window.showErrorMessage("Error copying URL: " + error); |
97 | | - } |
98 | | - |
99 | | - const infoMessage = await vscode.window.showInformationMessage( |
100 | | - snippetUrl, |
101 | | - "Open URL" |
102 | | - ); |
103 | | - if (infoMessage === "Open URL") { |
104 | | - vscode.env.openExternal(vscode.Uri.parse(snippetUrl)); |
105 | | - } |
106 | | - } |
107 | | -} |
108 | | - |
109 | | -function activate(context: vscode.ExtensionContext) { |
110 | | - let disposable = vscode.commands.registerCommand( |
111 | | - "extension.postSnippet", |
112 | | - async () => { |
113 | | - const editor = vscode.window.activeTextEditor; |
114 | | - if (!editor) { |
115 | | - vscode.window.showErrorMessage("No editor found"); |
116 | | - return; |
117 | | - } |
118 | | - |
119 | | - const text = getSelectedText(editor); |
120 | | - if (!text) { |
121 | | - return; |
122 | | - } |
123 | | - |
124 | | - const postData: PostData = { |
125 | | - title: getCurrentFileName() || "untitled", |
126 | | - code: text, |
127 | | - language: getLanguageIdentifier() || "python", |
128 | | - style: "androidstudio", |
129 | | - }; |
130 | | - |
131 | | - try { |
132 | | - const data = await postSnippet(postData); |
133 | | - await handlePostResponse(data); |
134 | | - } catch (error: any) { |
135 | | - vscode.window.showErrorMessage("Error: " + error.message); |
136 | | - } |
137 | | - } |
138 | | - ); |
139 | | - |
140 | | - const loginCommand = vscode.commands.registerCommand( |
141 | | - "extension.login", |
142 | | - () => { |
143 | | - login(); |
144 | | - } |
145 | | - ); |
146 | | - |
147 | | - context.subscriptions.push(disposable); |
| 1 | +/** |
| 2 | + * MIT License |
| 3 | + * Copyright (c) [2024] [Codeishot] |
| 4 | + * Giovanni D'Andrea => @gdjohn4s |
| 5 | + * Flavio Adamo => @FlavioAdamo |
| 6 | + * Gianluca Andretta => @gnlca |
| 7 | + * |
| 8 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | + * of this software and associated documentation files (the "Software"), to deal |
| 10 | + * in the Software without restriction, including without limitation the rights |
| 11 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | + * copies of the Software, and to permit persons to whom the Software is |
| 13 | + * furnished to do so, subject to the following conditions: |
| 14 | + * |
| 15 | + * The above copyright notice and this permission notice shall be included in all |
| 16 | + * copies or substantial portions of the Software. |
| 17 | + * |
| 18 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 24 | + * SOFTWARE. |
| 25 | + */ |
| 26 | + |
| 27 | +import { loginCommand, createSnippetCommand } from "./commands"; |
| 28 | +import { type ExtensionContext } from "vscode"; |
| 29 | +import { loginUriHandler } from "./handlers"; |
| 30 | + |
| 31 | +function activate(context: ExtensionContext) { |
| 32 | + context.subscriptions.push(createSnippetCommand); |
148 | 33 | context.subscriptions.push(loginCommand); |
149 | | - context.subscriptions.push( |
150 | | - // vscode://codeishot.codeishot/login?jwt=<token> |
151 | | - // TODO: @Cleanup => Move to another file, maybe `handlers.ts`? |
152 | | - vscode.window.registerUriHandler({ |
153 | | - handleUri(uri: vscode.Uri) { |
154 | | - if (uri.path === "/login") { |
155 | | - const jwtParam = uri.query.split("=")[1]; |
156 | | - if (jwtParam) { |
157 | | - vscode.window.showInformationMessage("Logged Successfully! ✨"); |
158 | | - saveToken(jwtParam); |
159 | | - } |
160 | | - } |
161 | | - }, |
162 | | - }) |
163 | | - ); |
| 34 | + context.subscriptions.push(loginUriHandler); |
164 | 35 | } |
165 | 36 |
|
166 | 37 | function deactivate() {} |
|
0 commit comments