From c862afedea863ef85488cd64a9378c90b1ef8222 Mon Sep 17 00:00:00 2001 From: Alex | Kronox Date: Tue, 12 Mar 2024 17:09:09 +0100 Subject: [PATCH] add unknown language option --- .../src/components/fileDisplaying/CodePanel.vue | 4 ++-- .../src/components/fileDisplaying/FilesContainer.vue | 4 ++-- report-viewer/src/model/CliOptions.ts | 4 ++-- report-viewer/src/model/Language.ts | 9 ++++++--- report-viewer/src/model/Overview.ts | 6 +++--- report-viewer/src/utils/CodeHighlighter.ts | 6 +++--- report-viewer/src/viewWrapper/ComparisonViewWrapper.vue | 4 ++-- report-viewer/src/views/ComparisonView.vue | 4 ++-- 8 files changed, 22 insertions(+), 19 deletions(-) diff --git a/report-viewer/src/components/fileDisplaying/CodePanel.vue b/report-viewer/src/components/fileDisplaying/CodePanel.vue index 938b45f82..506936f51 100644 --- a/report-viewer/src/components/fileDisplaying/CodePanel.vue +++ b/report-viewer/src/components/fileDisplaying/CodePanel.vue @@ -71,7 +71,7 @@ import Interactable from '../InteractableComponent.vue' import type { Match } from '@/model/Match' import type { SubmissionFile } from '@/model/File' import { highlight } from '@/utils/CodeHighlighter' -import type { ParserLanguage } from '@/model/Language' +import type { Language } from '@/model/Language' import { getMatchColor } from '@/utils/ColorUtils' import ToolTipComponent from '../ToolTipComponent.vue' @@ -94,7 +94,7 @@ const props = defineProps({ * Language of the file. */ highlightLanguage: { - type: String as PropType, + type: String as PropType, required: true } }) diff --git a/report-viewer/src/components/fileDisplaying/FilesContainer.vue b/report-viewer/src/components/fileDisplaying/FilesContainer.vue index eb1ce11ff..58c394e4e 100644 --- a/report-viewer/src/components/fileDisplaying/FilesContainer.vue +++ b/report-viewer/src/components/fileDisplaying/FilesContainer.vue @@ -48,7 +48,7 @@ import type { MatchInSingleFile } from '@/model/MatchInSingleFile' import { FontAwesomeIcon } from '@fortawesome/vue-fontawesome' import { faCompressAlt } from '@fortawesome/free-solid-svg-icons' import { library } from '@fortawesome/fontawesome-svg-core' -import type { ParserLanguage } from '@/model/Language' +import type { Language } from '@/model/Language' library.add(faCompressAlt) @@ -78,7 +78,7 @@ const props = defineProps({ * Language of the files. */ highlightLanguage: { - type: String as PropType, + type: String as PropType, required: true } }) diff --git a/report-viewer/src/model/CliOptions.ts b/report-viewer/src/model/CliOptions.ts index 2aee1ec78..266d69310 100644 --- a/report-viewer/src/model/CliOptions.ts +++ b/report-viewer/src/model/CliOptions.ts @@ -1,8 +1,8 @@ -import type { ParserLanguage } from './Language' +import type { Language } from './Language' import type { MetricType } from './MetricType' export interface CliOptions { - language: ParserLanguage + language: Language minTokenMatch: number submissionDirectories: string[] oldDirectories: string[] diff --git a/report-viewer/src/model/Language.ts b/report-viewer/src/model/Language.ts index 1334d56e1..198f28399 100644 --- a/report-viewer/src/model/Language.ts +++ b/report-viewer/src/model/Language.ts @@ -26,19 +26,22 @@ enum ParserLanguage { TYPESCRIPT = 'Typescript Parser' } +type Language = ParserLanguage | 'unknown language' + /** * Gets the LanguageParser enum value for the given language * @param language String representation of language the files were parsed with * @returns The LanguageParser enum value */ -function getLanguageParser(language: string): ParserLanguage { +function getLanguageParser(language: string): Language { for (const key in ParserLanguage) { if (ParserLanguage[key as keyof typeof ParserLanguage] === language) { return ParserLanguage[key as keyof typeof ParserLanguage] } } - throw new Error(`Language ${language} not found`) + console.warn(`Unknown language: ${language}\nCode highlighting might not work correctly.`) + return 'unknown language' } -export { ParserLanguage, getLanguageParser } +export { ParserLanguage, type Language, getLanguageParser } diff --git a/report-viewer/src/model/Overview.ts b/report-viewer/src/model/Overview.ts index ede6f93e9..56841636f 100644 --- a/report-viewer/src/model/Overview.ts +++ b/report-viewer/src/model/Overview.ts @@ -2,7 +2,7 @@ import type { Distribution } from './Distribution' import type { Cluster } from '@/model/Cluster' import type { ComparisonListElement } from './ComparisonListElement' import type { MetricType } from './MetricType' -import type { ParserLanguage } from './Language' +import type { Language } from './Language' /** * Model of the Overview file generated by JPlag @@ -10,7 +10,7 @@ import type { ParserLanguage } from './Language' export class Overview { private readonly _submissionFolderPath: Array private readonly _baseCodeFolderPath: string - private readonly _language: ParserLanguage + private readonly _language: Language private readonly _fileExtensions: Array private readonly _matchSensitivity: number private readonly _dateOfExecution: string @@ -23,7 +23,7 @@ export class Overview { constructor( submissionFolderPath: Array, baseCodeFolderPath: string, - language: ParserLanguage, + language: Language, fileExtensions: Array, matchSensitivity: number, dateOfExecution: string, diff --git a/report-viewer/src/utils/CodeHighlighter.ts b/report-viewer/src/utils/CodeHighlighter.ts index 913e6d959..54caa2a76 100644 --- a/report-viewer/src/utils/CodeHighlighter.ts +++ b/report-viewer/src/utils/CodeHighlighter.ts @@ -1,4 +1,4 @@ -import { ParserLanguage } from '@/model/Language' +import { type Language, ParserLanguage } from '@/model/Language' import hljs from 'highlight.js' import scheme from 'highlight.js/lib/languages/scheme' import llvm from 'highlight.js/lib/languages/llvm' @@ -13,7 +13,7 @@ import typescript from 'highlight.js/lib/languages/typescript' * @param lang Language to highlight the code with * @returns */ -export function highlight(code: string, lang: ParserLanguage) { +export function highlight(code: string, lang: Language) { const highlightedCode = hljs.highlight(code, { language: getHighlightLanguage(lang) }).value const openTags: string[] = [] const formattedCode = highlightedCode @@ -34,7 +34,7 @@ export function highlight(code: string, lang: ParserLanguage) { return formattedCode } -function getHighlightLanguage(lang: ParserLanguage) { +function getHighlightLanguage(lang: Language) { switch (lang) { case ParserLanguage.PYTHON: return 'python' diff --git a/report-viewer/src/viewWrapper/ComparisonViewWrapper.vue b/report-viewer/src/viewWrapper/ComparisonViewWrapper.vue index 59be445e1..02318c9ed 100644 --- a/report-viewer/src/viewWrapper/ComparisonViewWrapper.vue +++ b/report-viewer/src/viewWrapper/ComparisonViewWrapper.vue @@ -18,7 +18,7 @@ import type { Comparison } from '@/model/Comparison' import { ComparisonFactory } from '@/model/factories/ComparisonFactory' import LoadingCircle from '@/components/LoadingCircle.vue' import { redirectOnError } from '@/router' -import type { ParserLanguage } from '@/model/Language' +import type { Language } from '@/model/Language' import RepositoryReference from '@/components/RepositoryReference.vue' const props = defineProps({ @@ -29,7 +29,7 @@ const props = defineProps({ }) const comparison: Ref = ref(null) -const language: Ref = ref(null) +const language: Ref = ref(null) // This eslint rule is disabled to allow the use of await in the setup function. Disabling this rule is safe, because the props are gathered from the url, so changing them would reload the pafe anyway. // eslint-disable-next-line vue/no-setup-props-reactivity-loss diff --git a/report-viewer/src/views/ComparisonView.vue b/report-viewer/src/views/ComparisonView.vue index d84a8b266..0d3904462 100644 --- a/report-viewer/src/views/ComparisonView.vue +++ b/report-viewer/src/views/ComparisonView.vue @@ -113,7 +113,7 @@ import MatchList from '@/components/fileDisplaying/MatchList.vue' import FilesContainer from '@/components/fileDisplaying/FilesContainer.vue' import { store } from '@/stores/store' import Container from '@/components/ContainerComponent.vue' -import { ParserLanguage } from '@/model/Language' +import type { Language } from '@/model/Language' import hljsLightMode from 'highlight.js/styles/vs.css?raw' import hljsDarkMode from 'highlight.js/styles/vs2015.css?raw' import { MetricType } from '@/model/MetricType' @@ -129,7 +129,7 @@ const props = defineProps({ required: true }, language: { - type: Object as PropType, + type: Object as PropType, required: true } })