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
|
### Fixed
|
||||||
- Handling of snapshot plugin dependencies ([#1384](https://github.com/scm-manager/scm-manager/pull/1384))
|
- 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
|
## [2.6.3] - 2020-10-16
|
||||||
### Fixed
|
### Fixed
|
||||||
|
|||||||
@@ -43,7 +43,7 @@ storiesOf("SyntaxHighlighter", module)
|
|||||||
))
|
))
|
||||||
.add("Go", () => (
|
.add("Go", () => (
|
||||||
<Spacing>
|
<Spacing>
|
||||||
<SyntaxHighlighter language="go" value={GoHttpServer} />
|
<SyntaxHighlighter language="golang" value={GoHttpServer} />
|
||||||
</Spacing>
|
</Spacing>
|
||||||
))
|
))
|
||||||
.add("Javascript", () => (
|
.add("Javascript", () => (
|
||||||
|
|||||||
@@ -24,6 +24,7 @@
|
|||||||
import React from "react";
|
import React from "react";
|
||||||
|
|
||||||
import { PrismAsyncLight as ReactSyntaxHighlighter } from "react-syntax-highlighter";
|
import { PrismAsyncLight as ReactSyntaxHighlighter } from "react-syntax-highlighter";
|
||||||
|
import { defaultLanguage, determineLanguage } from "./languages";
|
||||||
// eslint-disable-next-line no-restricted-imports
|
// eslint-disable-next-line no-restricted-imports
|
||||||
import highlightingTheme from "./syntax-highlighting";
|
import highlightingTheme from "./syntax-highlighting";
|
||||||
|
|
||||||
@@ -33,27 +34,20 @@ type Props = {
|
|||||||
showLineNumbers?: boolean;
|
showLineNumbers?: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
const defaultLanguage = "text";
|
|
||||||
|
|
||||||
class SyntaxHighlighter extends React.Component<Props> {
|
class SyntaxHighlighter extends React.Component<Props> {
|
||||||
static defaultProps: Partial<Props> = {
|
static defaultProps: Partial<Props> = {
|
||||||
language: defaultLanguage,
|
language: defaultLanguage,
|
||||||
showLineNumbers: true
|
showLineNumbers: true
|
||||||
};
|
};
|
||||||
|
|
||||||
getLanguage = () => {
|
|
||||||
const { language } = this.props;
|
|
||||||
if (language) {
|
|
||||||
return language;
|
|
||||||
}
|
|
||||||
return defaultLanguage;
|
|
||||||
};
|
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
const { showLineNumbers } = this.props;
|
const { showLineNumbers, language } = this.props;
|
||||||
const language = this.getLanguage();
|
|
||||||
return (
|
return (
|
||||||
<ReactSyntaxHighlighter showLineNumbers={showLineNumbers} language={language} style={highlightingTheme}>
|
<ReactSyntaxHighlighter
|
||||||
|
showLineNumbers={showLineNumbers}
|
||||||
|
language={determineLanguage(language)}
|
||||||
|
style={highlightingTheme}
|
||||||
|
>
|
||||||
{this.props.value}
|
{this.props.value}
|
||||||
</ReactSyntaxHighlighter>
|
</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
|
// @ts-ignore we have no typings for react-diff-view
|
||||||
import { Diff, useTokenizeWorker } from "react-diff-view";
|
import { Diff, useTokenizeWorker } from "react-diff-view";
|
||||||
import { File } from "./DiffTypes";
|
import { File } from "./DiffTypes";
|
||||||
|
import { determineLanguage } from "../languages";
|
||||||
|
|
||||||
// styling for the diff tokens
|
// styling for the diff tokens
|
||||||
// this must be aligned with th style, which is used in the SyntaxHighlighter component
|
// this must be aligned with th style, which is used in the SyntaxHighlighter component
|
||||||
@@ -86,17 +87,10 @@ type Props = {
|
|||||||
className?: string;
|
className?: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
const determineLanguage = (file: File) => {
|
|
||||||
if (file.language) {
|
|
||||||
return file.language.toLowerCase();
|
|
||||||
}
|
|
||||||
return "text";
|
|
||||||
};
|
|
||||||
|
|
||||||
const TokenizedDiffView: FC<Props> = ({ file, viewType, className, children }) => {
|
const TokenizedDiffView: FC<Props> = ({ file, viewType, className, children }) => {
|
||||||
const { tokens } = useTokenizeWorker(tokenize, {
|
const { tokens } = useTokenizeWorker(tokenize, {
|
||||||
hunks: file.hunks,
|
hunks: file.hunks,
|
||||||
language: determineLanguage(file)
|
language: determineLanguage(file.language)
|
||||||
});
|
});
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|||||||
@@ -67,7 +67,7 @@ const commitImplementMain = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const source: AnnotatedSource = {
|
const source: AnnotatedSource = {
|
||||||
language: "go",
|
language: "golang",
|
||||||
lines: [
|
lines: [
|
||||||
{
|
{
|
||||||
lineNumber: 1,
|
lineNumber: 1,
|
||||||
|
|||||||
@@ -35,6 +35,7 @@ import { DateInput } from "../../useDateFormatter";
|
|||||||
import Popover from "./Popover";
|
import Popover from "./Popover";
|
||||||
import AnnotateLine from "./AnnotateLine";
|
import AnnotateLine from "./AnnotateLine";
|
||||||
import { Action } from "./actions";
|
import { Action } from "./actions";
|
||||||
|
import { determineLanguage } from "../../languages";
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
source: AnnotatedSource;
|
source: AnnotatedSource;
|
||||||
@@ -147,7 +148,7 @@ const Annotate: FC<Props> = ({ source, repository, baseDate }) => {
|
|||||||
{popover}
|
{popover}
|
||||||
<ReactSyntaxHighlighter
|
<ReactSyntaxHighlighter
|
||||||
showLineNumbers={false}
|
showLineNumbers={false}
|
||||||
language={source.language ? source.language : "text"}
|
language={determineLanguage(source.language)}
|
||||||
style={highlightingTheme}
|
style={highlightingTheme}
|
||||||
renderer={defaultRenderer}
|
renderer={defaultRenderer}
|
||||||
>
|
>
|
||||||
|
|||||||
Reference in New Issue
Block a user