Search in sources :

Example 1 with LanguageVersion

use of org.jetbrains.kotlin.config.LanguageVersion in project kotlin by JetBrains.

the class AbstractDiagnosticMessageTest method doTest.

public void doTest(String filePath) throws Exception {
    File file = new File(filePath);
    String fileName = file.getName();
    String fileData = KotlinTestUtils.doLoadFile(file);
    Map<String, String> directives = KotlinTestUtils.parseDirectives(fileData);
    int diagnosticNumber = getDiagnosticNumber(directives);
    final Set<DiagnosticFactory<?>> diagnosticFactories = getDiagnosticFactories(directives);
    MessageType messageType = getMessageTypeDirective(directives);
    String explicitLanguageVersion = InTextDirectivesUtils.findStringWithPrefixes(fileData, "// LANGUAGE_VERSION:");
    LanguageVersion version = explicitLanguageVersion == null ? null : LanguageVersion.fromVersionString(explicitLanguageVersion);
    KtFile psiFile = KotlinTestUtils.createFile(fileName, KotlinTestUtils.doLoadFile(getTestDataPath(), fileName), getProject());
    AnalysisResult analysisResult = analyze(psiFile, version);
    BindingContext bindingContext = analysisResult.getBindingContext();
    List<Diagnostic> diagnostics = ContainerUtil.filter(bindingContext.getDiagnostics().all(), new Condition<Diagnostic>() {

        @Override
        public boolean value(Diagnostic diagnostic) {
            return diagnosticFactories.contains(diagnostic.getFactory());
        }
    });
    assertEquals("Expected diagnostics number mismatch:", diagnosticNumber, diagnostics.size());
    int index = 1;
    String name = FileUtil.getNameWithoutExtension(fileName);
    for (Diagnostic diagnostic : diagnostics) {
        String readableDiagnosticText;
        String extension;
        if (messageType != MessageType.TEXT && IdeErrorMessages.hasIdeSpecificMessage(diagnostic)) {
            readableDiagnosticText = FormatHtmlUtilKt.formatHtml(IdeErrorMessages.render(diagnostic));
            extension = MessageType.HTML.extension;
        } else {
            readableDiagnosticText = DefaultErrorMessages.render(diagnostic);
            extension = MessageType.TEXT.extension;
        }
        String errorMessageFileName = name + index;
        String path = getTestDataPath() + "/" + errorMessageFileName + "." + extension;
        String actualText = "<!-- " + errorMessageFileName + " -->\n" + readableDiagnosticText;
        assertSameLinesWithFile(path, actualText);
        index++;
    }
}
Also used : Diagnostic(org.jetbrains.kotlin.diagnostics.Diagnostic) BindingContext(org.jetbrains.kotlin.resolve.BindingContext) AnalysisResult(org.jetbrains.kotlin.analyzer.AnalysisResult) DiagnosticFactory(org.jetbrains.kotlin.diagnostics.DiagnosticFactory) LanguageVersion(org.jetbrains.kotlin.config.LanguageVersion) KtFile(org.jetbrains.kotlin.psi.KtFile) KtFile(org.jetbrains.kotlin.psi.KtFile) File(java.io.File)

Example 2 with LanguageVersion

use of org.jetbrains.kotlin.config.LanguageVersion in project intellij by bazelbuild.

the class BlazeKotlinSyncPlugin method maybeUpdateCompilerConfig.

/**
 * Update the compiler settings of the project if needed. The language setting applies to both the
 * api version and the language version. Blanket setting this project wide is fine. The rules
 * should catch incorrect usage.
 */
private static void maybeUpdateCompilerConfig(Project project, ProjectViewSet projectViewSet) {
    LanguageVersion languageLevel = BlazeKotlinLanguageVersionSection.getLanguageLevel(projectViewSet);
    String languageLevelVersionString = languageLevel.getVersionString();
    CommonCompilerArguments settings = CommonCompilerArgumentsCompatUtils.getUnfrozenSettings(project);
    boolean updated = false;
    String apiVersion = CommonCompilerArgumentsCompatUtils.getApiVersion(settings);
    String languageVersion = CommonCompilerArgumentsCompatUtils.getLanguageVersion(settings);
    if (apiVersion == null || !apiVersion.equals(languageLevelVersionString)) {
        updated = true;
        CommonCompilerArgumentsCompatUtils.setApiVersion(settings, languageLevelVersionString);
    }
    if (languageVersion == null || !languageVersion.equals(languageLevelVersionString)) {
        updated = true;
        CommonCompilerArgumentsCompatUtils.setLanguageVersion(settings, languageLevelVersionString);
    }
    if (updated) {
        KotlinCommonCompilerArgumentsHolder.Companion.getInstance(project).setSettings(settings);
    }
}
Also used : LanguageVersion(org.jetbrains.kotlin.config.LanguageVersion) CommonCompilerArguments(org.jetbrains.kotlin.cli.common.arguments.CommonCompilerArguments)

Aggregations

LanguageVersion (org.jetbrains.kotlin.config.LanguageVersion)2 File (java.io.File)1 AnalysisResult (org.jetbrains.kotlin.analyzer.AnalysisResult)1 CommonCompilerArguments (org.jetbrains.kotlin.cli.common.arguments.CommonCompilerArguments)1 Diagnostic (org.jetbrains.kotlin.diagnostics.Diagnostic)1 DiagnosticFactory (org.jetbrains.kotlin.diagnostics.DiagnosticFactory)1 KtFile (org.jetbrains.kotlin.psi.KtFile)1 BindingContext (org.jetbrains.kotlin.resolve.BindingContext)1