Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix data type keywords in ST highlighting #991

Merged
merged 2 commits into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,22 @@ public final class Messages extends NLS {
public static String STCoreCustomValidatorConfigurationBlock_UnnecessaryWideConversion;
public static String STCoreCustomValidatorConfigurationBlock_WrongNameCase;
public static String STCoreEditorPreferencePage_PerformanceModeThreshold;
public static String STCoreHighlightingConfiguration_AlgorithmKeyword;
public static String STCoreHighlightingConfiguration_DataTypes;
public static String STCoreHighlightingConfiguration_FunctionBlockCalls;
public static String STCoreHighlightingConfiguration_FunctionBlockKeyword;
public static String STCoreHighlightingConfiguration_FunctionCalls;
public static String STCoreHighlightingConfiguration_FunctionName;
public static String STCoreHighlightingConfiguration_FunctionReturnVariables;
public static String STCoreHighlightingConfiguration_Functions;
public static String STCoreHighlightingConfiguration_GlobalConstants;
public static String STCoreHighlightingConfiguration_LocalVariables;
public static String STCoreHighlightingConfiguration_MemberVariables;
public static String STCoreHighlightingConfiguration_MethodCalls;
public static String STCoreHighlightingConfiguration_MethodKeyword;
public static String STCoreHighlightingConfiguration_MethodName;
public static String STCoreHighlightingConfiguration_MethodReturnVariables;
public static String STCoreHighlightingConfiguration_VariableKeyword;
public static String STCoreHoverProvider_EventKind;
public static String STCoreHoverProvider_FBKind;
public static String STCoreHoverProvider_FBTypeKind;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,22 @@ STCoreCustomValidatorConfigurationBlock_UnnecessaryNarrowConversion=Unnecessary
STCoreCustomValidatorConfigurationBlock_UnnecessaryWideConversion=Unnecessary wide conversion
STCoreCustomValidatorConfigurationBlock_WrongNameCase=Name of referenced feature has different case
STCoreEditorPreferencePage_PerformanceModeThreshold=Performance mode threshold in lines
STCoreHighlightingConfiguration_AlgorithmKeyword=Algorithm Keyword
STCoreHighlightingConfiguration_DataTypes=Data Types
STCoreHighlightingConfiguration_FunctionBlockCalls=Function Block Calls
STCoreHighlightingConfiguration_FunctionBlockKeyword=Function Block Keyword
STCoreHighlightingConfiguration_FunctionCalls=Function Calls
STCoreHighlightingConfiguration_FunctionName=Function Name
STCoreHighlightingConfiguration_FunctionReturnVariables=Function Return Variables
STCoreHighlightingConfiguration_Functions=Functions
STCoreHighlightingConfiguration_GlobalConstants=Global Constants
STCoreHighlightingConfiguration_LocalVariables=Local Variables
STCoreHighlightingConfiguration_MemberVariables=Member Variables
STCoreHighlightingConfiguration_MethodCalls=Method Calls
STCoreHighlightingConfiguration_MethodKeyword=Method Keyword
STCoreHighlightingConfiguration_MethodName=Method Name
STCoreHighlightingConfiguration_MethodReturnVariables=Method Return Variables
STCoreHighlightingConfiguration_VariableKeyword=Variable Keyword
STCoreHoverProvider_EventKind=EVENT
STCoreHoverProvider_FBKind=FUNCTION_BLOCK
STCoreHoverProvider_FBTypeKind=FUNCTION_BLOCK
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/**
* Copyright (c) 2022, 2025 Primetals Technologies Austria GmbH
* Martin Erich Jobst
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Hesam Rezaee
* - initial API and implementation and/or initial documentation
* Martin Jobst
* - fix data type keywords
*/
package org.eclipse.fordiac.ide.structuredtextcore.ui.syntaxcoloring;

import java.util.Set;
import java.util.stream.Collectors;

import org.eclipse.fordiac.ide.model.data.DataType;
import org.eclipse.fordiac.ide.model.typelibrary.DataTypeLibrary;
import org.eclipse.xtext.ide.editor.syntaxcoloring.HighlightingStyles;
import org.eclipse.xtext.ui.editor.syntaxcoloring.DefaultAntlrTokenToAttributeIdMapper;

@SuppressWarnings("nls")
public class STCoreAntlrTokenToAttributeIdMapper extends DefaultAntlrTokenToAttributeIdMapper {
private static final Set<String> VAR_DECLARATION_KEYWORDS = Set.of("\'VAR\'", "\'VAR_TEMP\'", "\'VAR_INPUT\'",
"\'VAR_OUTPUT\'", "\'VAR_IN_OUT\'", "\'END_VAR\'", "\'VAR_EXTERNAL\'", "\'VAR_GLOBAL\'", "\'VAR_ACCESS\'",
"\'RETAIN\'", "\'CONSTANT\'", "\'AT\'");
private static final Set<String> DATA_TYPES_KEYWORDS = DataTypeLibrary.getNonUserDefinedDataTypes().stream()
.map(DataType::getName).map(name -> "'" + name + "'").collect(Collectors.toUnmodifiableSet());
private static final Set<String> FUNCTIONS_KEYWORDS = Set.of("\'FUNCTION\'", "\'END_FUNCTION\'");
private static final Set<String> FUNCTION_BLOCK_KEYWORDS = Set.of("\'FUNCTION_BLOCK\'", "\'END_FUNCTION_BLOCK\'");
private static final Set<String> METHOD_BLOCK_KEYWORDS = Set.of("\'METHOD\'", "\'END_METHOD\'");
private static final Set<String> ALGORITHM_BLOCK_KEYWORDS = Set.of("\'ALGORITHM\'", "\'END_ALGORITHM\'");
private static final Set<String> BOOLEAN_KEYWORDS = Set.of("\'TRUE\'", "\'FALSE\'");

@Override
protected String calculateId(final String tokenName, final int tokenType) {
if (VAR_DECLARATION_KEYWORDS.contains(tokenName)) {
return STCoreHighlightingStyles.VAR_KEYWORD_ID;
}
if (DATA_TYPES_KEYWORDS.contains(tokenName)) {
return STCoreHighlightingStyles.DATA_TYPE_ID;
}
if (FUNCTION_BLOCK_KEYWORDS.contains(tokenName)) {
return STCoreHighlightingStyles.FUNCTION_BLOCK_ID;
}
if (METHOD_BLOCK_KEYWORDS.contains(tokenName)) {
return STCoreHighlightingStyles.METHOD_BLOCK_ID;
}
if (ALGORITHM_BLOCK_KEYWORDS.contains(tokenName)) {
return STCoreHighlightingStyles.ALGORITHM_BLOCK_ID;
}
if (FUNCTIONS_KEYWORDS.contains(tokenName)) {
return STCoreHighlightingStyles.FUNCTIONS_ID;
}
if (BOOLEAN_KEYWORDS.contains(tokenName)) {
return HighlightingStyles.NUMBER_ID;
}
return super.calculateId(tokenName, tokenType);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
/**
* Copyright (c) 2022, 2023 Primetals Technologies Austria GmbH
* Martin Erich Jobst
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Hesam Rezaee
* - initial API and implementation and/or initial documentation
* Martin Jobst
* - distinguish variable scope
*/
package org.eclipse.fordiac.ide.structuredtextcore.ui.syntaxcoloring;

import org.eclipse.fordiac.ide.structuredtextcore.ui.Messages;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.xtext.ui.editor.syntaxcoloring.DefaultHighlightingConfiguration;
import org.eclipse.xtext.ui.editor.syntaxcoloring.IHighlightingConfigurationAcceptor;
import org.eclipse.xtext.ui.editor.utils.TextStyle;

@SuppressWarnings("java:S4144")
public class STCoreHighlightingConfiguration extends DefaultHighlightingConfiguration {
public static final String VAR_KEYWORD_ID = STCoreHighlightingStyles.VAR_KEYWORD_ID;
public static final String DATA_TYPE_ID = STCoreHighlightingStyles.DATA_TYPE_ID;
public static final String FUNCTION_BLOCK_ID = STCoreHighlightingStyles.FUNCTION_BLOCK_ID;
public static final String METHOD_BLOCK_ID = STCoreHighlightingStyles.METHOD_BLOCK_ID;
public static final String ALGORITHM_BLOCK_ID = STCoreHighlightingStyles.ALGORITHM_BLOCK_ID;
public static final String FUNCTIONS_ID = STCoreHighlightingStyles.FUNCTIONS_ID;
public static final String GLOBAL_CONST_ID = STCoreHighlightingStyles.GLOBAL_CONST_ID;
public static final String MEMBER_VARIABLE_ID = STCoreHighlightingStyles.MEMBER_VARIABLE_ID;
public static final String LOCAL_VARIABLE_ID = STCoreHighlightingStyles.LOCAL_VARIABLE_ID;
public static final String CALL_FUNCTION_ID = STCoreHighlightingStyles.CALL_FUNCTION_ID;
public static final String CALL_FUNCTION_BLOCK_ID = STCoreHighlightingStyles.CALL_FUNCTION_BLOCK_ID;
public static final String CALL_METHOD_ID = STCoreHighlightingStyles.CALL_METHOD_ID;
public static final String RETURN_FUNCTION_ID = STCoreHighlightingStyles.RETURN_FUNCTION_ID;
public static final String RETURN_METHOD_ID = STCoreHighlightingStyles.RETURN_METHOD_ID;
public static final String FUNCTIONS_NAME_ID = STCoreHighlightingStyles.FUNCTIONS_NAME_ID;
public static final String METHODS_NAME_ID = STCoreHighlightingStyles.METHODS_NAME_ID;

@Override
public void configure(final IHighlightingConfigurationAcceptor acceptor) {
super.configure(acceptor);

acceptor.acceptDefaultHighlighting(VAR_KEYWORD_ID, Messages.STCoreHighlightingConfiguration_VariableKeyword,
varKeywordTextStyle());
acceptor.acceptDefaultHighlighting(DATA_TYPE_ID, Messages.STCoreHighlightingConfiguration_DataTypes,
dataTypeTextStyle());
acceptor.acceptDefaultHighlighting(FUNCTIONS_ID, Messages.STCoreHighlightingConfiguration_Functions,
functionsTextStyle());
acceptor.acceptDefaultHighlighting(METHOD_BLOCK_ID, Messages.STCoreHighlightingConfiguration_MethodKeyword,
methodBlockTextStyle());
acceptor.acceptDefaultHighlighting(ALGORITHM_BLOCK_ID,
Messages.STCoreHighlightingConfiguration_AlgorithmKeyword, algorithmTextStyle());
acceptor.acceptDefaultHighlighting(FUNCTION_BLOCK_ID,
Messages.STCoreHighlightingConfiguration_FunctionBlockKeyword, functionBockTextStyle());
acceptor.acceptDefaultHighlighting(CALL_FUNCTION_ID, Messages.STCoreHighlightingConfiguration_FunctionCalls,
callFunctionTextStyle());
acceptor.acceptDefaultHighlighting(CALL_FUNCTION_BLOCK_ID,
Messages.STCoreHighlightingConfiguration_FunctionBlockCalls, callFunctionBlockTextStyle());
acceptor.acceptDefaultHighlighting(CALL_METHOD_ID, Messages.STCoreHighlightingConfiguration_MethodCalls,
callMethodTextStyle());
acceptor.acceptDefaultHighlighting(GLOBAL_CONST_ID, Messages.STCoreHighlightingConfiguration_GlobalConstants,
globalConstTextStyle());
acceptor.acceptDefaultHighlighting(MEMBER_VARIABLE_ID, Messages.STCoreHighlightingConfiguration_MemberVariables,
memberVarTextStyle());
acceptor.acceptDefaultHighlighting(LOCAL_VARIABLE_ID, Messages.STCoreHighlightingConfiguration_LocalVariables,
localVarTextStyle());
acceptor.acceptDefaultHighlighting(RETURN_FUNCTION_ID,
Messages.STCoreHighlightingConfiguration_FunctionReturnVariables, returnFunctionTextStyle());
acceptor.acceptDefaultHighlighting(RETURN_METHOD_ID,
Messages.STCoreHighlightingConfiguration_MethodReturnVariables, returnMethodTextStyle());
acceptor.acceptDefaultHighlighting(FUNCTIONS_NAME_ID, Messages.STCoreHighlightingConfiguration_FunctionName,
functionNameTextStyle());
acceptor.acceptDefaultHighlighting(METHODS_NAME_ID, Messages.STCoreHighlightingConfiguration_MethodName,
methodNameTextStyle());
}

public TextStyle algorithmTextStyle() {
final TextStyle textStyle = defaultTextStyle().copy();
textStyle.setColor(new RGB(128, 0, 128));
textStyle.setStyle(SWT.BOLD);
return textStyle;
}

public TextStyle callFunctionTextStyle() {
final TextStyle textStyle = defaultTextStyle().copy();
textStyle.setColor(new RGB(0, 128, 255));
return textStyle;
}

public TextStyle callFunctionBlockTextStyle() {
final TextStyle textStyle = defaultTextStyle().copy();
textStyle.setColor(new RGB(0, 128, 255));
return textStyle;
}

public TextStyle callMethodTextStyle() {
final TextStyle textStyle = defaultTextStyle().copy();
textStyle.setColor(new RGB(0, 128, 255));
return textStyle;
}

public TextStyle dataTypeTextStyle() {
final TextStyle textStyle = defaultTextStyle().copy();
textStyle.setColor(new RGB(128, 128, 255));
return textStyle;
}

public TextStyle functionBockTextStyle() {
final TextStyle textStyle = defaultTextStyle().copy();
textStyle.setColor(new RGB(128, 0, 128));
textStyle.setStyle(SWT.BOLD);
return textStyle;
}

public TextStyle functionsTextStyle() {
final TextStyle textStyle = defaultTextStyle().copy();
textStyle.setColor(new RGB(128, 0, 128));
textStyle.setStyle(SWT.BOLD);
return textStyle;
}

public TextStyle functionNameTextStyle() {
return defaultTextStyle();
}

public TextStyle methodNameTextStyle() {
return defaultTextStyle();
}

@Override
public TextStyle keywordTextStyle() {
final TextStyle textStyle = defaultTextStyle().copy();
textStyle.setColor(new RGB(255, 128, 0));
return textStyle;
}

public TextStyle methodBlockTextStyle() {
final TextStyle textStyle = defaultTextStyle().copy();
textStyle.setColor(new RGB(128, 0, 128));
textStyle.setStyle(SWT.BOLD);
return textStyle;
}

@Override
public TextStyle numberTextStyle() {
final TextStyle textStyle = defaultTextStyle().copy();
textStyle.setColor(new RGB(0, 164, 164));
return textStyle;
}

public TextStyle returnFunctionTextStyle() {
return defaultTextStyle();
}

public TextStyle returnMethodTextStyle() {
return defaultTextStyle();
}

public TextStyle globalConstTextStyle() {
final TextStyle textStyle = defaultTextStyle().copy();
textStyle.setColor(new RGB(141, 218, 248));
textStyle.setStyle(SWT.BOLD | SWT.ITALIC);
return textStyle;
}

public TextStyle memberVarTextStyle() {
final TextStyle textStyle = defaultTextStyle().copy();
textStyle.setColor(new RGB(128, 64, 64));
return textStyle;
}

public TextStyle localVarTextStyle() {
return defaultTextStyle();
}

@Override
public TextStyle stringTextStyle() {
final TextStyle textStyle = defaultTextStyle().copy();
textStyle.setColor(new RGB(0, 0, 255));
return textStyle;
}

public TextStyle varKeywordTextStyle() {
final TextStyle textStyle = defaultTextStyle().copy();
textStyle.setColor(new RGB(128, 128, 255));
textStyle.setStyle(SWT.BOLD);
return textStyle;
}
}
Loading
Loading