|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2024 Bachmann electronic GmbH and others. |
| 3 | + * |
| 4 | + * This program and the accompanying materials are made |
| 5 | + * available under the terms of the Eclipse Public License 2.0 |
| 6 | + * which is available at https://www.eclipse.org/legal/epl-2.0/ |
| 7 | + * |
| 8 | + * SPDX-License-Identifier: EPL-2.0 |
| 9 | + * |
| 10 | + * Contributors: |
| 11 | + * Gesa Hentschke (Bachmann electronic GmbH) - initial implementation |
| 12 | + *******************************************************************************/ |
| 13 | + |
| 14 | +package org.eclipse.cdt.lsp.internal.clangd; |
| 15 | + |
| 16 | +import java.io.IOException; |
| 17 | + |
| 18 | +import org.eclipse.cdt.lsp.internal.clangd.editor.ClangdPlugin; |
| 19 | +import org.eclipse.core.resources.IFile; |
| 20 | +import org.eclipse.core.resources.IMarker; |
| 21 | +import org.eclipse.core.resources.IResource; |
| 22 | +import org.eclipse.core.runtime.CoreException; |
| 23 | +import org.eclipse.core.runtime.Platform; |
| 24 | +import org.yaml.snakeyaml.Yaml; |
| 25 | +import org.yaml.snakeyaml.error.MarkedYAMLException; |
| 26 | + |
| 27 | +/** |
| 28 | + * Checks the <code>.clangd</code> file for syntax errors and notifies the user via error markers in the file and Problems view. |
| 29 | + */ |
| 30 | +public class ClangdConfigFileChecker { |
| 31 | + public static final String CLANGD_MARKER = ClangdPlugin.PLUGIN_ID + ".config.marker"; //$NON-NLS-1$ |
| 32 | + |
| 33 | + /** |
| 34 | + * Checks if the .clangd file contains valid yaml syntax. Adds error marker to the file if not. |
| 35 | + * @param configFile |
| 36 | + */ |
| 37 | + public void checkConfigFile(IFile configFile) { |
| 38 | + if (!configFile.exists()) { |
| 39 | + return; |
| 40 | + } |
| 41 | + Yaml yaml = new Yaml(); |
| 42 | + try (var inputStream = configFile.getContents()) { |
| 43 | + try { |
| 44 | + removeMarkerFromClangdConfig(configFile); |
| 45 | + //throws ScannerException and ParserException: |
| 46 | + yaml.load(inputStream); |
| 47 | + } catch (MarkedYAMLException yamlException) { |
| 48 | + // re-read the file, because the buffer which comes along with MarkedYAMLException is limited to ~800 bytes. |
| 49 | + try (var reReadStream = configFile.getContents()) { |
| 50 | + addMarkerToClangdConfig(configFile, yamlException, reReadStream.readAllBytes()); |
| 51 | + } |
| 52 | + } catch (Exception exception) { |
| 53 | + //log unexpected exception: |
| 54 | + Platform.getLog(getClass()).error("Expected MarkedYAMLException, but was: " + exception.getMessage(), //$NON-NLS-1$ |
| 55 | + exception); |
| 56 | + } |
| 57 | + } catch (IOException | CoreException e) { |
| 58 | + Platform.getLog(getClass()).error(e.getMessage(), e); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + private void addMarkerToClangdConfig(IFile configFile, MarkedYAMLException yamlException, byte[] buffer) { |
| 63 | + try { |
| 64 | + var configMarker = parseYamlException(yamlException, buffer); |
| 65 | + var marker = configFile.createMarker(CLANGD_MARKER); |
| 66 | + marker.setAttribute(IMarker.MESSAGE, configMarker.message); |
| 67 | + marker.setAttribute(IMarker.SEVERITY, IMarker.SEVERITY_ERROR); |
| 68 | + marker.setAttribute(IMarker.LINE_NUMBER, configMarker.line); |
| 69 | + marker.setAttribute(IMarker.CHAR_START, configMarker.charStart); |
| 70 | + marker.setAttribute(IMarker.CHAR_END, configMarker.charEnd); |
| 71 | + } catch (CoreException core) { |
| 72 | + Platform.getLog(getClass()).log(core.getStatus()); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + private void removeMarkerFromClangdConfig(IFile configFile) { |
| 77 | + try { |
| 78 | + configFile.deleteMarkers(CLANGD_MARKER, false, IResource.DEPTH_ZERO); |
| 79 | + } catch (CoreException e) { |
| 80 | + Platform.getLog(getClass()).log(e.getStatus()); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + private class ClangdConfigMarker { |
| 85 | + public String message; |
| 86 | + public int line = 1; |
| 87 | + public int charStart = -1; |
| 88 | + public int charEnd = -1; |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Fetch line and char position information from exception to create a marker for the .clangd file. |
| 93 | + * @param exception |
| 94 | + * @param file |
| 95 | + * @return |
| 96 | + */ |
| 97 | + private ClangdConfigMarker parseYamlException(MarkedYAMLException exception, byte[] buffer) { |
| 98 | + var marker = new ClangdConfigMarker(); |
| 99 | + marker.message = exception.getProblem(); |
| 100 | + var problemMark = exception.getProblemMark(); |
| 101 | + if (problemMark == null) { |
| 102 | + return marker; |
| 103 | + } |
| 104 | + marker.line = problemMark.getLine() + 1; //getLine() is zero based, IMarker wants 1-based |
| 105 | + int index = problemMark.getIndex(); |
| 106 | + if (index == buffer.length) { |
| 107 | + // When index == buffer.length() the marker index points to the non visible |
| 108 | + // \r or \n character and the marker is not displayed in the editor. |
| 109 | + // Or, even worse, there is no next line and index + 1 would be > buffer.length |
| 110 | + // Therefore we have to find the last visible char: |
| 111 | + index = getIndexOfLastVisibleChar(buffer); |
| 112 | + } |
| 113 | + marker.charStart = index; |
| 114 | + marker.charEnd = index + 1; |
| 115 | + return marker; |
| 116 | + } |
| 117 | + |
| 118 | + private int getIndexOfLastVisibleChar(byte[] buffer) { |
| 119 | + for (int i = buffer.length - 1; i >= 0; i--) { |
| 120 | + if ('\r' != ((char) buffer[i]) && '\n' != ((char) buffer[i])) { |
| 121 | + return i; |
| 122 | + } |
| 123 | + } |
| 124 | + return Math.max(0, buffer.length - 2); |
| 125 | + } |
| 126 | +} |
0 commit comments