Skip to content

Commit f1b1a88

Browse files
feat: add colour highlight for codesnippet (hoppscotch#5006)
Co-authored-by: jamesgeorge007 <[email protected]>
1 parent 236db83 commit f1b1a88

File tree

3 files changed

+62
-3
lines changed

3 files changed

+62
-3
lines changed

packages/hoppscotch-common/src/components/http/Codegen.vue

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
@click="
4646
() => {
4747
codegenType = codegen.name
48+
codegenMode = codegen.lang
4849
hide()
4950
}
5051
"
@@ -124,6 +125,7 @@ import {
124125
CodegenDefinitions,
125126
CodegenName,
126127
generateCode,
128+
CodegenLang,
127129
} from "~/helpers/new-codegen"
128130
import {
129131
getEffectiveRESTRequest,
@@ -171,6 +173,7 @@ const currentActiveTabDocument = computed(() =>
171173
)
172174
173175
const codegenType = ref<CodegenName>("shell-curl")
176+
const codegenMode = ref<CodegenLang>("shell")
174177
const errorState = ref(false)
175178
176179
defineProps({
@@ -289,7 +292,7 @@ useCodemirror(
289292
requestCode,
290293
reactive({
291294
extendedEditorConfig: {
292-
mode: "text/plain",
295+
mode: codegenMode,
293296
readOnly: true,
294297
lineWrapping: WRAP_LINES,
295298
},

packages/hoppscotch-common/src/composables/codemirror.ts

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,16 @@ import { GQLLanguage } from "@hoppscotch/codemirror-lang-graphql"
3030
import { html } from "@codemirror/legacy-modes/mode/xml"
3131
import { shell } from "@codemirror/legacy-modes/mode/shell"
3232
import { yaml } from "@codemirror/legacy-modes/mode/yaml"
33+
import { rust } from "@codemirror/legacy-modes/mode/rust"
34+
import { go } from "@codemirror/legacy-modes/mode/go"
35+
import { clojure } from "@codemirror/legacy-modes/mode/clojure"
36+
import { http } from "@codemirror/legacy-modes/mode/http"
37+
import { csharp, java } from "@codemirror/legacy-modes/mode/clike"
38+
import { powerShell } from "@codemirror/legacy-modes/mode/powershell"
39+
import { python } from "@codemirror/legacy-modes/mode/python"
40+
import { r } from "@codemirror/legacy-modes/mode/r"
41+
import { ruby } from "@codemirror/legacy-modes/mode/ruby"
42+
import { swift } from "@codemirror/legacy-modes/mode/swift"
3343
import { isJSONContentType } from "@helpers/utils/contenttypes"
3444
import { useStreamSubscriber } from "@composables/stream"
3545
import { Completer } from "@helpers/editor/completion"
@@ -157,10 +167,45 @@ const hoppLang = (
157167
return language ? new LanguageSupport(language, exts) : exts
158168
}
159169

170+
/**
171+
* Map of language MIME types to their corresponding language definitions
172+
* where the import name matches the langMime string exactly.
173+
* These are used with `StreamLanguage.define(...)` to register the language.
174+
*/
175+
const streamLanguageMap: Record<string, any> = {
176+
rust,
177+
clojure,
178+
csharp,
179+
go,
180+
http,
181+
java,
182+
powershell: powerShell,
183+
python,
184+
shell,
185+
html,
186+
r,
187+
ruby,
188+
swift,
189+
}
190+
191+
/**
192+
* Returns the appropriate CodeMirror language object based on the provided MIME type.
193+
*
194+
* Handles specific content types like JSON, JavaScript, GraphQL, XML, etc.
195+
* For simpler languages that directly match the import name, uses a lookup map
196+
* to reduce repetition and automatically defines the StreamLanguage.
197+
*
198+
* @param langMime - The MIME type or shorthand language identifier (e.g., "javascript", "go", "python")
199+
* @returns The corresponding CodeMirror Language object
200+
*/
160201
const getLanguage = (langMime: string): Language | null => {
202+
// Special case for JSON types
161203
if (isJSONContentType(langMime)) {
162204
return jsoncLanguage
163-
} else if (langMime === "application/javascript") {
205+
} else if (
206+
langMime === "application/javascript" ||
207+
langMime === "javascript"
208+
) {
164209
return javascriptLanguage
165210
} else if (langMime === "graphql") {
166211
return GQLLanguage
@@ -174,7 +219,13 @@ const getLanguage = (langMime: string): Language | null => {
174219
return StreamLanguage.define(yaml)
175220
}
176221

177-
// None matched, so return null
222+
// Handle cases where langMime directly matches the import name
223+
const streamLang = streamLanguageMap[langMime]
224+
if (streamLang) {
225+
return StreamLanguage.define(streamLang)
226+
}
227+
228+
// If no match is found, return null
178229
return null
179230
}
180231

packages/hoppscotch-common/src/helpers/new-codegen/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,11 @@ export const CodegenDefinitions = [
193193
*/
194194
export type CodegenName = (typeof CodegenDefinitions)[number]["name"]
195195

196+
/**
197+
* A type which defines all the valid code generator languages
198+
*/
199+
export type CodegenLang = (typeof CodegenDefinitions)[number]["lang"]
200+
196201
/**
197202
* Generates Source Code for the given codgen
198203
* @param codegen The codegen to apply

0 commit comments

Comments
 (0)