<commit_before>/*
 * AceEditorMixins.java
 *
 * Copyright (C) 2020 by RStudio, PBC
 *
 * Unless you have received this program directly from RStudio pursuant
 * to the terms of a commercial license agreement with RStudio, then
 * this program is licensed to you under the terms of version 3 of the
 * GNU Affero General Public License. This program is distributed WITHOUT
 * ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT,
 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the
 * AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details.
 *
 */
package org.rstudio.studio.client.workbench.views.source.editors.text;

import org.rstudio.core.client.BrowseCap;
import org.rstudio.core.client.CommandWithArg;
import org.rstudio.core.client.StringUtil;
import org.rstudio.studio.client.JavaScriptEventHistory;
import org.rstudio.studio.client.RStudioGinjector;
import org.rstudio.studio.client.JavaScriptEventHistory.EventData;
import org.rstudio.studio.client.application.Desktop;
import org.rstudio.studio.client.workbench.views.source.editors.text.ace.AceEditorNative;
import org.rstudio.studio.client.workbench.views.source.editors.text.events.AceSelectionChangedEvent;

import com.google.inject.Inject;

public class AceEditorMixins
{
   public AceEditorMixins(AceEditor editor)
   {
      RStudioGinjector.INSTANCE.injectMembers(this);
      
      editor_ = editor.getWidget().getEditor();
      
      initializeMixins(editor_);
      
      // on Linux, record whenever the user selects something in Ace
      if (BrowseCap.isLinuxDesktop())
      {
         editor.addSelectionChangedHandler(new AceSelectionChangedEvent.Handler()
         {
            @Override
            public void onSelectionChanged(AceSelectionChangedEvent event)
            {
               String selection = editor_.getSelectedText();
               if (!StringUtil.isNullOrEmpty(selection))
                  Desktop.getFrame().setGlobalMouseSelection(selection);
            }
         });
      }
   }
   
   @Inject
   private void initialize(JavaScriptEventHistory history)
   {
      history_ = history;
   }
   
   private final native void initializeMixins(AceEditorNative editor)
   /*-{
      // store reference to mixins object
      var self = this;
      
      // override the 'onPaste()' method provided by the Editor prototype
      editor.$onPaste = editor.onPaste;
      
      editor.onPaste = $entry(function(text) {
         
         // call mixins method
         self.@org.rstudio.studio.client.workbench.views.source.editors.text.AceEditorMixins::onPaste(Lorg/rstudio/studio/client/workbench/views/source/editors/text/ace/AceEditorNative;Ljava/lang/String;)(this, text);<commit_msg>defer setting of global mouse selection (#8061)<commit_after>/*
 * AceEditorMixins.java
 *
 * Copyright (C) 2020 by RStudio, PBC
 *
 * Unless you have received this program directly from RStudio pursuant
 * to the terms of a commercial license agreement with RStudio, then
 * this program is licensed to you under the terms of version 3 of the
 * GNU Affero General Public License. This program is distributed WITHOUT
 * ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT,
 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the
 * AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details.
 *
 */
package org.rstudio.studio.client.workbench.views.source.editors.text;

import org.rstudio.core.client.BrowseCap;
import org.rstudio.core.client.CommandWithArg;
import org.rstudio.core.client.StringUtil;
import org.rstudio.studio.client.JavaScriptEventHistory;
import org.rstudio.studio.client.RStudioGinjector;
import org.rstudio.studio.client.JavaScriptEventHistory.EventData;
import org.rstudio.studio.client.application.Desktop;
import org.rstudio.studio.client.workbench.views.source.editors.text.ace.AceEditorNative;
import org.rstudio.studio.client.workbench.views.source.editors.text.events.AceSelectionChangedEvent;

import com.google.gwt.core.client.Scheduler;
import com.google.inject.Inject;

public class AceEditorMixins
{
   public AceEditorMixins(AceEditor editor)
   {
      RStudioGinjector.INSTANCE.injectMembers(this);
      
      editor_ = editor.getWidget().getEditor();
      
      initializeMixins(editor_);
      
      // on Linux, record whenever the user selects something in Ace
      if (BrowseCap.isLinuxDesktop())
      {
         editor.addSelectionChangedHandler(new AceSelectionChangedEvent.Handler()
         {
            @Override
            public void onSelectionChanged(AceSelectionChangedEvent event)
            {
               String selection = editor_.getSelectedText();
               if (!StringUtil.isNullOrEmpty(selection))
               {
                  Scheduler.get().scheduleDeferred(() ->
                  {
                     Desktop.getFrame().setGlobalMouseSelection(selection);
                  });
               }
            }
         });
      }
   }
   
   @Inject
   private void initialize(JavaScriptEventHistory history)
   {
      history_ = history;
   }
   
   private final native void initializeMixins(AceEditorNative editor)
   /*-{
      // store reference to mixins object
      var self = this;
      
      // override the 'onPaste()' method provided by the Editor prototype
      editor.$onPaste = editor.onPaste;
      
      editor.onPaste = $entry(function(text) {
         
         // call mixins method
         self.@org.rstudio.studio.client.workbench.views.source.editors.text.AceEditorMixins::onPaste(Lorg/rstudio/studio/client/workbench/views/source/editors/text/ace/AceEditorNative;Ljava/lang/String;)(this, text);
{'fraction_non_alphanumeric': 0.09082938804138173, 'fraction_numerical': 0.003156233561283535, 'mean_word_length': 3.0626780626780628, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 2, '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': '5633410', 'n_tokens_mistral': 1676, 'n_tokens_neox': 1614, 'n_words': 356}