1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-08-13 19:25:38 +02:00

Bug 540257 - "Align Const" save action causes NPE when saving Assembler files

Change-Id: Ie3a086ddc7bc79eaef259a570327408e098bef51
Signed-off-by: Marc-Andre Laperle <malaperle@gmail.com>
This commit is contained in:
Marc-Andre Laperle 2018-10-20 00:19:09 -04:00 committed by Marc-André Laperle
parent 8404a76e6e
commit 08721bdf8e
4 changed files with 67 additions and 3 deletions

View file

@ -0,0 +1,59 @@
/*******************************************************************************
* Copyright (c) 2018 Marc-Andre Laperle.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*******************************************************************************/
package org.eclipse.cdt.ui.tests.text;
import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.preferences.DefaultScope;
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.core.testplugin.CProjectHelper;
import org.eclipse.cdt.ui.CUIPlugin;
import org.eclipse.cdt.ui.PreferenceConstants;
import org.eclipse.cdt.ui.testplugin.EditorTestHelper;
import org.eclipse.cdt.internal.ui.editor.asm.AsmTextEditor;
import junit.framework.TestCase;
/**
* Tests for the AsmTextEditor
*/
public class AsmTextEditorTest extends TestCase {
private static final String ASM_EDITOR_ID = "org.eclipse.cdt.ui.editor.asm.AsmEditor";
private ICProject fCProject;
@Override
protected void setUp() throws Exception {
fCProject = CProjectHelper.createCCProject("AsmTextEditorTestProject", "");
}
@Override
protected void tearDown() throws Exception {
if (fCProject != null)
CProjectHelper.delete(fCProject);
}
public void testAlignConstSaveAction_Bug11111() throws Exception {
IEclipsePreferences prefs = DefaultScope.INSTANCE.getNode(CUIPlugin.PLUGIN_ID);
boolean oldValue = prefs.getBoolean(PreferenceConstants.ALIGN_ALL_CONST, false);
prefs.putBoolean(PreferenceConstants.ALIGN_ALL_CONST, true);
ByteArrayInputStream stream = new ByteArrayInputStream("".getBytes(StandardCharsets.UTF_8));
String filename = "test.asm";
IFile file = fCProject.getProject().getFile(filename);
file.create(stream, true, null);
AsmTextEditor editor = (AsmTextEditor) EditorTestHelper.openInEditor(file, ASM_EDITOR_ID, true);
editor.doSave(null);
EditorTestHelper.closeEditor(editor);
prefs.putBoolean(PreferenceConstants.ALIGN_ALL_CONST, oldValue);
}
}

View file

@ -28,6 +28,7 @@ import org.eclipse.cdt.ui.tests.text.doctools.DocCommentTestSuite;
// partitioning tests
PartitionTokenScannerTest.class,
CPartitionerTest.class,
AsmTextEditorTest.class,
AsmPartitionerTest.class,
// smart edit tests

View file

@ -141,8 +141,10 @@ public class AlignConstAction extends TextEditorAction {
try {
IASTTranslationUnit ast = translationUnit.getAST(null, ITranslationUnit.AST_SKIP_ALL_HEADERS);
IASTNode enclosingNode = ast.getNodeSelector(null).findEnclosingNode(offset, length);
rewriteMisalignedConstSpecifiers(enclosingNode, new NullProgressMonitor());
if (ast != null) {
IASTNode enclosingNode = ast.getNodeSelector(null).findEnclosingNode(offset, length);
rewriteMisalignedConstSpecifiers(enclosingNode, new NullProgressMonitor());
}
} catch (CoreException e) {
CUIPlugin.log(e);
}

View file

@ -32,7 +32,9 @@ public class AlignConstSaveAction {
private void alignConstInActiveEditor(ITranslationUnit translationUnit, IProgressMonitor monitor) {
try {
IASTTranslationUnit ast = translationUnit.getAST(null, ITranslationUnit.AST_SKIP_ALL_HEADERS);
AlignConstAction.rewriteMisalignedConstSpecifiers(ast, monitor);
if (ast != null) {
AlignConstAction.rewriteMisalignedConstSpecifiers(ast, monitor);
}
} catch (CoreException e) {
CUIPlugin.log(e);
}