diff --git a/CHANGELOG.md b/CHANGELOG.md index 3b140e5226..4ed668b487 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed - Handling of snapshot plugin dependencies ([#1384](https://github.com/scm-manager/scm-manager/pull/1384)) +- SyntaxHighlighting for GoLang ([#1386](https://github.com/scm-manager/scm-manager/pull/1386)) ## [2.6.3] - 2020-10-16 ### Fixed diff --git a/scm-ui/ui-components/src/SyntaxHighlighter.stories.tsx b/scm-ui/ui-components/src/SyntaxHighlighter.stories.tsx index 7f458e4784..16331dcfd2 100644 --- a/scm-ui/ui-components/src/SyntaxHighlighter.stories.tsx +++ b/scm-ui/ui-components/src/SyntaxHighlighter.stories.tsx @@ -43,7 +43,7 @@ storiesOf("SyntaxHighlighter", module) )) .add("Go", () => ( - + )) .add("Javascript", () => ( diff --git a/scm-ui/ui-components/src/SyntaxHighlighter.tsx b/scm-ui/ui-components/src/SyntaxHighlighter.tsx index 852335e161..b665041913 100644 --- a/scm-ui/ui-components/src/SyntaxHighlighter.tsx +++ b/scm-ui/ui-components/src/SyntaxHighlighter.tsx @@ -24,6 +24,7 @@ import React from "react"; import { PrismAsyncLight as ReactSyntaxHighlighter } from "react-syntax-highlighter"; +import { defaultLanguage, determineLanguage } from "./languages"; // eslint-disable-next-line no-restricted-imports import highlightingTheme from "./syntax-highlighting"; @@ -33,27 +34,20 @@ type Props = { showLineNumbers?: boolean; }; -const defaultLanguage = "text"; - class SyntaxHighlighter extends React.Component { static defaultProps: Partial = { language: defaultLanguage, showLineNumbers: true }; - getLanguage = () => { - const { language } = this.props; - if (language) { - return language; - } - return defaultLanguage; - }; - render() { - const { showLineNumbers } = this.props; - const language = this.getLanguage(); + const { showLineNumbers, language } = this.props; return ( - + {this.props.value} ); diff --git a/scm-ui/ui-components/src/languages.test.ts b/scm-ui/ui-components/src/languages.test.ts new file mode 100644 index 0000000000..61a6214065 --- /dev/null +++ b/scm-ui/ui-components/src/languages.test.ts @@ -0,0 +1,52 @@ +/* + * MIT License + * + * Copyright (c) 2020-present Cloudogu GmbH and Contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +import { determineLanguage } from "./languages"; + +describe("syntax highlighter", () => { + it("should return the language as it is", () => { + const java = determineLanguage("java"); + expect(java).toBe("java"); + }); + + it("should lower case the language", () => { + const java = determineLanguage("Java"); + expect(java).toBe("java"); + }); + + it("should return text if language is undefied", () => { + const lang = determineLanguage(); + expect(lang).toBe("text"); + }); + + it("should return text if language is an empty string", () => { + const lang = determineLanguage(""); + expect(lang).toBe("text"); + }); + + it("should use alias go for golang", () => { + const go = determineLanguage("golang"); + expect(go).toBe("go"); + }); +}); diff --git a/scm-ui/ui-components/src/languages.ts b/scm-ui/ui-components/src/languages.ts new file mode 100644 index 0000000000..f895d9d946 --- /dev/null +++ b/scm-ui/ui-components/src/languages.ts @@ -0,0 +1,41 @@ +/* + * MIT License + * + * Copyright (c) 2020-present Cloudogu GmbH and Contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +// this aliases are only to map from spotter detection to prismjs +const languageAliases: { [key: string]: string } = { + golang: "go" +}; + +export const defaultLanguage = "text"; + +export const determineLanguage = (language?: string) => { + if (!language) { + return defaultLanguage; + } + const lang = language.toLowerCase(); + if (languageAliases[lang]) { + return languageAliases[lang]; + } + return lang; +}; diff --git a/scm-ui/ui-components/src/repos/TokenizedDiffView.tsx b/scm-ui/ui-components/src/repos/TokenizedDiffView.tsx index dcd1d65616..26e1d36455 100644 --- a/scm-ui/ui-components/src/repos/TokenizedDiffView.tsx +++ b/scm-ui/ui-components/src/repos/TokenizedDiffView.tsx @@ -26,6 +26,7 @@ import styled from "styled-components"; // @ts-ignore we have no typings for react-diff-view import { Diff, useTokenizeWorker } from "react-diff-view"; import { File } from "./DiffTypes"; +import { determineLanguage } from "../languages"; // styling for the diff tokens // this must be aligned with th style, which is used in the SyntaxHighlighter component @@ -86,17 +87,10 @@ type Props = { className?: string; }; -const determineLanguage = (file: File) => { - if (file.language) { - return file.language.toLowerCase(); - } - return "text"; -}; - const TokenizedDiffView: FC = ({ file, viewType, className, children }) => { const { tokens } = useTokenizeWorker(tokenize, { hunks: file.hunks, - language: determineLanguage(file) + language: determineLanguage(file.language) }); return ( diff --git a/scm-ui/ui-components/src/repos/annotate/Annotate.stories.tsx b/scm-ui/ui-components/src/repos/annotate/Annotate.stories.tsx index 41345b2744..2da808c387 100644 --- a/scm-ui/ui-components/src/repos/annotate/Annotate.stories.tsx +++ b/scm-ui/ui-components/src/repos/annotate/Annotate.stories.tsx @@ -67,7 +67,7 @@ const commitImplementMain = { }; const source: AnnotatedSource = { - language: "go", + language: "golang", lines: [ { lineNumber: 1, diff --git a/scm-ui/ui-components/src/repos/annotate/Annotate.tsx b/scm-ui/ui-components/src/repos/annotate/Annotate.tsx index 9aeb36dd55..7ed489af25 100644 --- a/scm-ui/ui-components/src/repos/annotate/Annotate.tsx +++ b/scm-ui/ui-components/src/repos/annotate/Annotate.tsx @@ -35,6 +35,7 @@ import { DateInput } from "../../useDateFormatter"; import Popover from "./Popover"; import AnnotateLine from "./AnnotateLine"; import { Action } from "./actions"; +import { determineLanguage } from "../../languages"; type Props = { source: AnnotatedSource; @@ -147,7 +148,7 @@ const Annotate: FC = ({ source, repository, baseDate }) => { {popover}