<commit_before>/*
 * 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 net.groboclown.idea.p4ic.v2.file;

import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.fileEditor.FileDocumentManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vcs.VcsException;
import com.intellij.openapi.vcs.changes.VcsDirtyScopeManager;
import com.intellij.openapi.vfs.VirtualFile;
import net.groboclown.idea.p4ic.actions.BasicAction;
import net.groboclown.idea.p4ic.compat.VcsCompat;
import net.groboclown.idea.p4ic.extension.P4Vcs;
import net.groboclown.idea.p4ic.server.exceptions.P4Exception;
import net.groboclown.idea.p4ic.v2.changes.P4ChangeListMapping;
import net.groboclown.idea.p4ic.v2.server.P4Server;
import org.jetbrains.annotations.NotNull;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

public class P4Edit extends BasicAction {
    private static final Logger LOG = Logger.getInstance(P4Edit.class);

    public static final String EDIT = "Edit files";


    @NotNull
    @Override
    protected String getActionName() {
        return EDIT;
    }

    @Override
    protected boolean isEnabled(@NotNull Project project, @NotNull P4Vcs vcs, @NotNull VirtualFile... vFiles) {
        // If an input file does not map to a server, then we still report enabled.  If all the files only map to
        // non-P4 files, then disable.

        if (LOG.isDebugEnabled()) {
            LOG.debug("Checking enabled state for files " + Arrays.asList(vFiles));
        }

        try {
            final Map<P4Server, List<VirtualFile>> servers = vcs.mapVirtualFilesToP4Server(Arrays.asList(vFiles));

            // all we care about for open for edit/add is whether
            // the files map to servers or not.  Online or offline
            // modes don't matter.
            return !(servers.isEmpty() || (servers.size() == 1 && servers.containsKey(null)));
        } catch (InterruptedException e) {
            LOG.info(e);
            return false;
        }
    }



    @Override
    protected void perform(@NotNull final Project project, @NotNull final P4Vcs vcs,
            @NotNull final List<VcsException> exceptions, @NotNull final List<VirtualFile> affectedFiles) {
        if (affectedFiles.isEmpty()) {
            return;
        }

        final Map<P4Server, List<VirtualFile>> servers;
        try {
            servers = vcs.mapVirtualFilesToP4Server(affectedFiles);
        } catch (InterruptedException e) {
            exceptions.add(new P4Exception(e));
            return;
        }

        FileDocumentManager.getInstance().saveAllDocuments();

        P4ChangeListMapping changeListMapping = P4ChangeListMapping.getInstance(project);

        LOG.info("adding or editing files: " + affectedFiles);

        for (Map.Entry<P4Server, List<VirtualFile>> en: servers.entrySet()) {
            final P4Server server = en.getKey();<commit_msg>Get rid of server config check on Edit menu display<commit_after>/*
 * 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 net.groboclown.idea.p4ic.v2.file;

import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.fileEditor.FileDocumentManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vcs.VcsException;
import com.intellij.openapi.vcs.changes.VcsDirtyScopeManager;
import com.intellij.openapi.vfs.VirtualFile;
import net.groboclown.idea.p4ic.actions.BasicAction;
import net.groboclown.idea.p4ic.compat.VcsCompat;
import net.groboclown.idea.p4ic.extension.P4Vcs;
import net.groboclown.idea.p4ic.server.exceptions.P4Exception;
import net.groboclown.idea.p4ic.v2.changes.P4ChangeListMapping;
import net.groboclown.idea.p4ic.v2.server.P4Server;
import org.jetbrains.annotations.NotNull;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

public class P4Edit extends BasicAction {
    private static final Logger LOG = Logger.getInstance(P4Edit.class);

    public static final String EDIT = "Edit files";


    @NotNull
    @Override
    protected String getActionName() {
        return EDIT;
    }

    @Override
    protected boolean isEnabled(@NotNull Project project, @NotNull P4Vcs vcs, @NotNull VirtualFile... vFiles) {
        // If an input file does not map to a server, then we still report enabled.  If all the files only map to
        // non-P4 files, then disable.

        if (LOG.isDebugEnabled()) {
            LOG.debug("Checking enabled state for files " + Arrays.asList(vFiles));
        }

        /*

        This seems to have trouble performing the correct calculations.
        Allow it to be shown, which will also skip the possible check
        and speed up the UI operation.  If the user tries to edit the
        file when it's not really possible, then the edit will handle the
        error conditions at that point.

        try {
            final Map<P4Server, List<VirtualFile>> servers = vcs.mapVirtualFilesToOnlineP4Server(Arrays.asList(vFiles));

            // all we care about for open for edit/add is whether
            // the files map to servers or not.  Online or offline
            // modes don't matter.
            return !(servers.isEmpty() || (servers.size() == 1 && servers.containsKey(null)));
        } catch (InterruptedException e) {
            LOG.info(e);
            return true;
        }

        */
        return true;
    }



    @Override
    protected void perform(@NotNull final Project project, @NotNull final P4Vcs vcs,
            @NotNull final List<VcsException> exceptions, @NotNull final List<VirtualFile> affectedFiles) {
        if (affectedFiles.isEmpty()) {
            return;
        }

        final Map<P4Server, List<VirtualFile>> servers;
        try {
            servers = vcs.mapVirtualFilesToP4Server(affectedFiles);
        } catch (InterruptedException e) {
            exceptions.add(new P4Exception(e));
            return;
        }

        FileDocumentManager.getInstance().saveAllDocuments();

        P4ChangeListMapping changeListMapping = P4ChangeListMapping.getInstance(project);

        LOG.info("adding or editing files: " + affectedFiles);

        for (Map.Entry<P4Server, List<VirtualFile>> en: servers.entrySet()) {
            final P4Server server = en.getKey();
{'fraction_non_alphanumeric': 0.09145756204579734, 'fraction_numerical': 0.00904977375565611, 'mean_word_length': 3.0165198237885464, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2399703', 'n_tokens_mistral': 2097, 'n_tokens_neox': 2047, 'n_words': 615}