Search in sources :

Example 1 with ExclusionItem

use of org.sonarlint.eclipse.core.internal.resources.ExclusionItem in project sonarlint-eclipse by SonarSource.

the class SonarLintGlobalConfigurationTest method should_serialize_file_exclusions.

@Test
public void should_serialize_file_exclusions() {
    var list = List.of(new ExclusionItem(Type.FILE, "file"), new ExclusionItem(Type.DIRECTORY, "dir"));
    var serialized = SonarLintGlobalConfiguration.serializeFileExclusions(list);
    var desList = SonarLintGlobalConfiguration.deserializeFileExclusions(serialized);
    assertThat(desList).isEqualTo(list);
}
Also used : ExclusionItem(org.sonarlint.eclipse.core.internal.resources.ExclusionItem) Test(org.junit.Test)

Example 2 with ExclusionItem

use of org.sonarlint.eclipse.core.internal.resources.ExclusionItem in project sonarlint-eclipse by SonarSource.

the class EditProjectExclusionDialog method selectFile.

/**
 * Opens a dialog where the user can select a file path.
 */
private void selectFile() {
    ISelectionStatusValidator validator = arr -> {
        if (arr.length > 1) {
            return ValidationStatus.error("Only one file can be selected");
        }
        if (arr.length == 0) {
            return ValidationStatus.ok();
        }
        Object obj = arr[0];
        ISonarLintFile file = Adapters.adapt(obj, ISonarLintFile.class);
        return file != null ? ValidationStatus.ok() : ValidationStatus.error("Select a file");
    };
    var viewFilter = new ViewerFilter() {

        @Override
        public boolean select(Viewer viewer, Object parentElement, Object element) {
            if (element instanceof IFolder) {
                var folder = (IFolder) element;
                return SonarLintUtils.isSonarLintFileCandidate(folder);
            }
            if (element instanceof IFile) {
                var file = Adapters.adapt(element, ISonarLintFile.class);
                return file != null;
            }
            return false;
        }
    };
    var lp = new WorkbenchLabelProvider();
    var cp = new WorkbenchContentProvider();
    var dialog = new ElementTreeSelectionDialog(getShell(), lp, cp);
    dialog.setTitle("Select File");
    dialog.setInput(project.getResource());
    dialog.setAllowMultiple(false);
    dialog.addFilter(viewFilter);
    dialog.setValidator(validator);
    dialog.setMessage("Select a project file to be excluded from SonarLint analysis");
    if (editItem != null) {
        dialog.setInitialSelection(editItem.item());
    }
    if (dialog.open() == Window.OK) {
        var obj = dialog.getFirstResult();
        var file = Adapters.adapt(obj, ISonarLintFile.class);
        if (file != null) {
            editItem = new ExclusionItem(Type.FILE, file.getProjectRelativePath());
            fileField.setText(editItem.item());
        }
    }
}
Also used : WorkbenchContentProvider(org.eclipse.ui.model.WorkbenchContentProvider) IFolder(org.eclipse.core.resources.IFolder) ExclusionItem(org.sonarlint.eclipse.core.internal.resources.ExclusionItem) ISelectionStatusValidator(org.eclipse.ui.dialogs.ISelectionStatusValidator) StringUtils(org.sonarlint.eclipse.core.internal.utils.StringUtils) Type(org.sonarlint.eclipse.core.internal.resources.ExclusionItem.Type) ValidationStatus(org.eclipse.core.databinding.validation.ValidationStatus) IMessageProvider(org.eclipse.jface.dialogs.IMessageProvider) Nullable(org.eclipse.jdt.annotation.Nullable) Composite(org.eclipse.swt.widgets.Composite) IFile(org.eclipse.core.resources.IFile) GridData(org.eclipse.swt.layout.GridData) ViewerFilter(org.eclipse.jface.viewers.ViewerFilter) WorkbenchLabelProvider(org.eclipse.ui.model.WorkbenchLabelProvider) ISonarLintProject(org.sonarlint.eclipse.core.resource.ISonarLintProject) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) Text(org.eclipse.swt.widgets.Text) PatternSyntaxException(java.util.regex.PatternSyntaxException) Shell(org.eclipse.swt.widgets.Shell) Viewer(org.eclipse.jface.viewers.Viewer) Button(org.eclipse.swt.widgets.Button) SonarLintUtils(org.sonarlint.eclipse.core.internal.utils.SonarLintUtils) Adapters(org.eclipse.core.runtime.Adapters) Window(org.eclipse.jface.window.Window) Dialog(org.eclipse.jface.dialogs.Dialog) ModifyListener(org.eclipse.swt.events.ModifyListener) Path(org.eclipse.core.runtime.Path) SWT(org.eclipse.swt.SWT) ISonarLintFile(org.sonarlint.eclipse.core.resource.ISonarLintFile) SelectionEvent(org.eclipse.swt.events.SelectionEvent) FileSystems(java.nio.file.FileSystems) Control(org.eclipse.swt.widgets.Control) ElementTreeSelectionDialog(org.eclipse.ui.dialogs.ElementTreeSelectionDialog) WorkbenchLabelProvider(org.eclipse.ui.model.WorkbenchLabelProvider) IFile(org.eclipse.core.resources.IFile) ViewerFilter(org.eclipse.jface.viewers.ViewerFilter) ISonarLintFile(org.sonarlint.eclipse.core.resource.ISonarLintFile) Viewer(org.eclipse.jface.viewers.Viewer) WorkbenchContentProvider(org.eclipse.ui.model.WorkbenchContentProvider) ElementTreeSelectionDialog(org.eclipse.ui.dialogs.ElementTreeSelectionDialog) ISelectionStatusValidator(org.eclipse.ui.dialogs.ISelectionStatusValidator) ExclusionItem(org.sonarlint.eclipse.core.internal.resources.ExclusionItem) IFolder(org.eclipse.core.resources.IFolder)

Example 3 with ExclusionItem

use of org.sonarlint.eclipse.core.internal.resources.ExclusionItem in project sonarlint-eclipse by SonarSource.

the class EditProjectExclusionDialog method selectFolder.

/**
 * Opens a dialog where the user can select a folder path.
 */
private void selectFolder() {
    ISelectionStatusValidator validator = arr -> {
        if (arr.length > 1) {
            return ValidationStatus.error("Only one folder can be selected");
        }
        if (arr.length == 0) {
            return ValidationStatus.ok();
        }
        var obj = arr[0];
        return (obj instanceof IFolder) ? ValidationStatus.ok() : ValidationStatus.error("Select a folder");
    };
    var viewFilter = new ViewerFilter() {

        @Override
        public boolean select(Viewer viewer, Object parentElement, Object element) {
            if (element instanceof IFolder) {
                IFolder folder = (IFolder) element;
                return SonarLintUtils.isSonarLintFileCandidate(folder);
            }
            return false;
        }
    };
    var lp = new WorkbenchLabelProvider();
    var cp = new WorkbenchContentProvider();
    var dialog = new ElementTreeSelectionDialog(getShell(), lp, cp);
    dialog.setTitle("Select Folder");
    dialog.setInput(project.getResource());
    dialog.setAllowMultiple(false);
    dialog.addFilter(viewFilter);
    dialog.setValidator(validator);
    dialog.setMessage("Select a project folder to be excluded from SonarLint analysis");
    if (editItem != null) {
        dialog.setInitialSelection(editItem.item());
    }
    if (dialog.open() == Window.OK) {
        var obj = dialog.getFirstResult();
        var folder = (IFolder) obj;
        if (folder != null) {
            editItem = new ExclusionItem(Type.DIRECTORY, folder.getProjectRelativePath().toString());
            directoryField.setText(editItem.item());
        }
    }
}
Also used : WorkbenchContentProvider(org.eclipse.ui.model.WorkbenchContentProvider) IFolder(org.eclipse.core.resources.IFolder) ExclusionItem(org.sonarlint.eclipse.core.internal.resources.ExclusionItem) ISelectionStatusValidator(org.eclipse.ui.dialogs.ISelectionStatusValidator) StringUtils(org.sonarlint.eclipse.core.internal.utils.StringUtils) Type(org.sonarlint.eclipse.core.internal.resources.ExclusionItem.Type) ValidationStatus(org.eclipse.core.databinding.validation.ValidationStatus) IMessageProvider(org.eclipse.jface.dialogs.IMessageProvider) Nullable(org.eclipse.jdt.annotation.Nullable) Composite(org.eclipse.swt.widgets.Composite) IFile(org.eclipse.core.resources.IFile) GridData(org.eclipse.swt.layout.GridData) ViewerFilter(org.eclipse.jface.viewers.ViewerFilter) WorkbenchLabelProvider(org.eclipse.ui.model.WorkbenchLabelProvider) ISonarLintProject(org.sonarlint.eclipse.core.resource.ISonarLintProject) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) Text(org.eclipse.swt.widgets.Text) PatternSyntaxException(java.util.regex.PatternSyntaxException) Shell(org.eclipse.swt.widgets.Shell) Viewer(org.eclipse.jface.viewers.Viewer) Button(org.eclipse.swt.widgets.Button) SonarLintUtils(org.sonarlint.eclipse.core.internal.utils.SonarLintUtils) Adapters(org.eclipse.core.runtime.Adapters) Window(org.eclipse.jface.window.Window) Dialog(org.eclipse.jface.dialogs.Dialog) ModifyListener(org.eclipse.swt.events.ModifyListener) Path(org.eclipse.core.runtime.Path) SWT(org.eclipse.swt.SWT) ISonarLintFile(org.sonarlint.eclipse.core.resource.ISonarLintFile) SelectionEvent(org.eclipse.swt.events.SelectionEvent) FileSystems(java.nio.file.FileSystems) Control(org.eclipse.swt.widgets.Control) ElementTreeSelectionDialog(org.eclipse.ui.dialogs.ElementTreeSelectionDialog) ElementTreeSelectionDialog(org.eclipse.ui.dialogs.ElementTreeSelectionDialog) WorkbenchLabelProvider(org.eclipse.ui.model.WorkbenchLabelProvider) ViewerFilter(org.eclipse.jface.viewers.ViewerFilter) ISelectionStatusValidator(org.eclipse.ui.dialogs.ISelectionStatusValidator) Viewer(org.eclipse.jface.viewers.Viewer) WorkbenchContentProvider(org.eclipse.ui.model.WorkbenchContentProvider) ExclusionItem(org.sonarlint.eclipse.core.internal.resources.ExclusionItem) IFolder(org.eclipse.core.resources.IFolder)

Example 4 with ExclusionItem

use of org.sonarlint.eclipse.core.internal.resources.ExclusionItem in project sonarlint-eclipse by SonarSource.

the class FileExclusionsPage method edit.

@Override
protected void edit() {
    var exclusion = (ExclusionItem) table.getStructuredSelection().getFirstElement();
    EditExclusionDialog dialog;
    if (isGlobal()) {
        dialog = new EditGlobalExclusionDialog(shell, exclusion.item());
    } else {
        dialog = new EditProjectExclusionDialog(getProject(), shell, exclusion);
    }
    // opens the dialog - just returns if the user cancels it
    if (dialog.open() == Window.CANCEL) {
        return;
    }
    var newExclusion = dialog.get();
    if (newExclusion != null) {
        var index = exclusions.indexOf(exclusion);
        exclusions.set(index, newExclusion);
        table.setSelection(new StructuredSelection(newExclusion));
        table.refresh();
    }
}
Also used : StructuredSelection(org.eclipse.jface.viewers.StructuredSelection) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) ExclusionItem(org.sonarlint.eclipse.core.internal.resources.ExclusionItem)

Aggregations

ExclusionItem (org.sonarlint.eclipse.core.internal.resources.ExclusionItem)4 FileSystems (java.nio.file.FileSystems)2 PatternSyntaxException (java.util.regex.PatternSyntaxException)2 ValidationStatus (org.eclipse.core.databinding.validation.ValidationStatus)2 IFile (org.eclipse.core.resources.IFile)2 IFolder (org.eclipse.core.resources.IFolder)2 Adapters (org.eclipse.core.runtime.Adapters)2 Path (org.eclipse.core.runtime.Path)2 Nullable (org.eclipse.jdt.annotation.Nullable)2 Dialog (org.eclipse.jface.dialogs.Dialog)2 IMessageProvider (org.eclipse.jface.dialogs.IMessageProvider)2 Viewer (org.eclipse.jface.viewers.Viewer)2 ViewerFilter (org.eclipse.jface.viewers.ViewerFilter)2 Window (org.eclipse.jface.window.Window)2 SWT (org.eclipse.swt.SWT)2 ModifyListener (org.eclipse.swt.events.ModifyListener)2 SelectionAdapter (org.eclipse.swt.events.SelectionAdapter)2 SelectionEvent (org.eclipse.swt.events.SelectionEvent)2 GridData (org.eclipse.swt.layout.GridData)2 Button (org.eclipse.swt.widgets.Button)2