/*
 * DBeaver - Universal Database Manager
 * Copyright (C) 2010-2021 DBeaver Corp and others
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jkiss.dbeaver.ui.editors.text.handlers;

import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.jface.text.*;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionProvider;
import org.eclipse.ui.handlers.HandlerUtil;
import org.jkiss.dbeaver.runtime.DBWorkbench;
import org.jkiss.dbeaver.ui.editors.text.BaseTextEditor;
import org.jkiss.utils.CommonUtils;

public class TrimTextSpacesHandler extends AbstractTextHandler {

    @Override
    public Object execute(ExecutionEvent executionEvent) throws ExecutionException {

        BaseTextEditor textEditor = BaseTextEditor.getTextEditor(HandlerUtil.getActiveEditor(executionEvent));

        if (textEditor != null) {
            ISelectionProvider provider = textEditor.getSelectionProvider();
            IDocument document = textEditor.getDocumentProvider().getDocument(textEditor.getEditorInput());
            if (provider != null && document != null) {
                ISelection selection = provider.getSelection();
                if (selection instanceof ITextSelection) {
                    ITextSelection textSelection = (ITextSelection) selection;
                    int offset = textSelection.getOffset();
                    if (textSelection.getLength() > 0) {
                        String trimmedSelection = textSelection.getText().trim();
                        if (!CommonUtils.isEmpty(trimmedSelection)) {
                            try {
                                document.replace(offset, textSelection.getLength(), trimmedSelection);
                            } catch (BadLocationException e) {
                                DBWorkbench.getPlatformUI().showError("Trim spaces", "Error replacing text", e);
                            }
                        }
                    } else if (offset > 0) {
                        try {
                            IRegion information = document.getLineInformationOfOffset(offset);
                            int startLine = information.getOffset();
                            int length = offset - startLine;
                            String untrimmedString = document.get(startLine, length);
                            String trimmedString = untrimmedString.trim();
                            if (!CommonUtils.isEmpty(untrimmedString)) {
                                document.replace(startLine, length, trimmedString);
                            }
                        } catch (BadLocationException e) {
                            DBWorkbench.getPlatformUI().showError("Trim spaces", "Error replacing text", e);
                        }
                    }
                }
            }
        }
        return null;
    }

}

@@ -1117,16 +1117,58 @@
 onUtils;
+%0Aimport org.jkiss.utils.StandardConstants;
 %0A%0Apublic
@@ -1223,24 +1223,120 @@
 tHandler %7B%0A%0A
+    private final String lineBreak = System.getProperty(StandardConstants.ENV_LINE_SEPARATOR);%0A%0A
     @Overrid
@@ -2121,32 +2121,1404 @@
 Length() %3E 0) %7B%0A
+                        int startLine = textSelection.getStartLine();%0A                        int endLine = textSelection.getEndLine();%0A                        if (startLine != endLine) %7B // Highlighted more than one line - make trim for each row separately%0A                            try %7B%0A                                StringBuilder allStrings = new StringBuilder();%0A                                for (int i = startLine; i %3C= endLine; i++) %7B%0A                                    IRegion lineInformation = document.getLineInformation(i);%0A                                    String untrimmedString = document.get(lineInformation.getOffset(), lineInformation.getLength());%0A                                    allStrings.append(untrimmedString.trim());%0A                                    if (i != endLine) %7B%0A                                        allStrings.append(lineBreak);%0A                                    %7D%0A                                %7D%0A                                document.replace(offset, textSelection.getLength(), allStrings.toString());%0A                            %7D catch (BadLocationException e) %7B%0A                                DBWorkbench.getPlatformUI().showError(%22Trim spaces%22, %22Error getting or replacing text%22, e);%0A                            %7D%0A                        %7D else %7B // Make trim only for the highlighted area of the string%0A    
                 
@@ -3575,32 +3575,36 @@
 tText().trim();%0A
+    
                 
@@ -3677,38 +3677,46 @@
                 
+  
+  
 try %7B%0A
+    
                 
@@ -3822,32 +3822,36 @@
                 
+    
 %7D catch (BadLoca
@@ -3893,32 +3893,36 @@
                 
+  
+  
 DBWorkbench.getP
@@ -4006,34 +4006,42 @@
                 
-%7D%0A
+    %7D%0A    
                 
@@ -4074,32 +4074,139 @@
     
-%7D else if (offset %3E 0) %7B
+    %7D%0A                    %7D else if (offset %3E 0) %7B // Nothing is highlighted - make trim only for the string on which the cursor is
 %0A   

#6052 make trim spaces for many strings