mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-13 08:55:44 +01:00
Fix syntax highlighting for go files
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -43,7 +43,7 @@ storiesOf("SyntaxHighlighter", module)
|
||||
))
|
||||
.add("Go", () => (
|
||||
<Spacing>
|
||||
<SyntaxHighlighter language="go" value={GoHttpServer} />
|
||||
<SyntaxHighlighter language="golang" value={GoHttpServer} />
|
||||
</Spacing>
|
||||
))
|
||||
.add("Javascript", () => (
|
||||
|
||||
@@ -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<Props> {
|
||||
static defaultProps: Partial<Props> = {
|
||||
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 (
|
||||
<ReactSyntaxHighlighter showLineNumbers={showLineNumbers} language={language} style={highlightingTheme}>
|
||||
<ReactSyntaxHighlighter
|
||||
showLineNumbers={showLineNumbers}
|
||||
language={determineLanguage(language)}
|
||||
style={highlightingTheme}
|
||||
>
|
||||
{this.props.value}
|
||||
</ReactSyntaxHighlighter>
|
||||
);
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
52
scm-ui/ui-components/src/languages.test.ts
Normal file
52
scm-ui/ui-components/src/languages.test.ts
Normal file
@@ -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");
|
||||
});
|
||||
});
|
||||
41
scm-ui/ui-components/src/languages.ts
Normal file
41
scm-ui/ui-components/src/languages.ts
Normal file
@@ -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;
|
||||
};
|
||||
@@ -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<Props> = ({ file, viewType, className, children }) => {
|
||||
const { tokens } = useTokenizeWorker(tokenize, {
|
||||
hunks: file.hunks,
|
||||
language: determineLanguage(file)
|
||||
language: determineLanguage(file.language)
|
||||
});
|
||||
|
||||
return (
|
||||
|
||||
@@ -67,7 +67,7 @@ const commitImplementMain = {
|
||||
};
|
||||
|
||||
const source: AnnotatedSource = {
|
||||
language: "go",
|
||||
language: "golang",
|
||||
lines: [
|
||||
{
|
||||
lineNumber: 1,
|
||||
|
||||
@@ -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<Props> = ({ source, repository, baseDate }) => {
|
||||
{popover}
|
||||
<ReactSyntaxHighlighter
|
||||
showLineNumbers={false}
|
||||
language={source.language ? source.language : "text"}
|
||||
language={determineLanguage(source.language)}
|
||||
style={highlightingTheme}
|
||||
renderer={defaultRenderer}
|
||||
>
|
||||
|
||||
Reference in New Issue
Block a user