<commit_before>/*
 * VisualModeNavigation.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.visualmode;

import org.rstudio.studio.client.RStudioGinjector;
import org.rstudio.studio.client.application.events.EventBus;
import org.rstudio.studio.client.panmirror.PanmirrorNavigation;
import org.rstudio.studio.client.panmirror.PanmirrorNavigationType;
import org.rstudio.studio.client.panmirror.PanmirrorWidget;
import org.rstudio.studio.client.panmirror.location.PanmirrorEditingLocation;
import org.rstudio.studio.client.workbench.views.source.events.SourceNavigationEvent;
import org.rstudio.studio.client.workbench.views.source.model.SourceNavigation;
import org.rstudio.studio.client.workbench.views.source.model.SourcePosition;

import com.google.inject.Inject;


public class VisualModeNavigation
{
   interface Context
   {
      String getId();
      String getPath();
      PanmirrorWidget panmirror();
   }
   
   public VisualModeNavigation(Context context)
   {
      RStudioGinjector.INSTANCE.injectMembers(this);
      context_ = context;
   }
   
   @Inject
   void initialize(EventBus events)
   {
      events_ = events;
   }
   
   
   public void onNavigated(PanmirrorNavigation navigation)
   {
      if (navigation.prevPos != -1)
         events_.fireEvent(new SourceNavigationEvent(createSourceNavigation(navigation.prevPos)));
      events_.fireEvent(new SourceNavigationEvent(createSourceNavigation(navigation.pos)));
   }
   
   public boolean isVisualModePosition(SourcePosition position)
   {
      return kPanmirrorContext.equals(position.getContext());
   }

   public void navigate(SourcePosition position, boolean recordCurrentPosition)
   {
      int pos = (kRowLength * position.getRow()) + position.getColumn();
      context_.panmirror().navigate(PanmirrorNavigationType.Pos, Integer.toString(pos), recordCurrentPosition);
   }
   
   public void navigateToXRef(String xref, boolean recordCurrentPosition)
   {
      context_.panmirror().navigate(PanmirrorNavigationType.XRef, xref, recordCurrentPosition);
   }
   
   public void recordCurrentNavigationPosition()
   {
      events_.fireEvent(new SourceNavigationEvent(createSourceNavigation(getSourcePosition())));            
   }
   
   public SourcePosition getSourcePosition()
   {
      PanmirrorWidget panmirror = context_.panmirror();
      if (panmirror != null)
      {
         PanmirrorEditingLocation editingLocation = panmirror.getEditingLocation();
         return createSourcePosition(editingLocation.pos);
      }
      else
      { 
         return createSourcePosition(0);
      }<commit_msg>only record visual editor navigation position if the editor is attached<commit_after>/*
 * VisualModeNavigation.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.visualmode;

import org.rstudio.studio.client.RStudioGinjector;
import org.rstudio.studio.client.application.events.EventBus;
import org.rstudio.studio.client.panmirror.PanmirrorNavigation;
import org.rstudio.studio.client.panmirror.PanmirrorNavigationType;
import org.rstudio.studio.client.panmirror.PanmirrorWidget;
import org.rstudio.studio.client.panmirror.location.PanmirrorEditingLocation;
import org.rstudio.studio.client.workbench.views.source.events.SourceNavigationEvent;
import org.rstudio.studio.client.workbench.views.source.model.SourceNavigation;
import org.rstudio.studio.client.workbench.views.source.model.SourcePosition;

import com.google.inject.Inject;


public class VisualModeNavigation
{
   interface Context
   {
      String getId();
      String getPath();
      PanmirrorWidget panmirror();
   }
   
   public VisualModeNavigation(Context context)
   {
      RStudioGinjector.INSTANCE.injectMembers(this);
      context_ = context;
   }
   
   @Inject
   void initialize(EventBus events)
   {
      events_ = events;
   }
   
   
   public void onNavigated(PanmirrorNavigation navigation)
   {
      if (navigation.prevPos != -1)
         events_.fireEvent(new SourceNavigationEvent(createSourceNavigation(navigation.prevPos)));
      events_.fireEvent(new SourceNavigationEvent(createSourceNavigation(navigation.pos)));
   }
   
   public boolean isVisualModePosition(SourcePosition position)
   {
      return kPanmirrorContext.equals(position.getContext());
   }

   public void navigate(SourcePosition position, boolean recordCurrentPosition)
   {
      int pos = (kRowLength * position.getRow()) + position.getColumn();
      context_.panmirror().navigate(PanmirrorNavigationType.Pos, Integer.toString(pos), recordCurrentPosition);
   }
   
   public void navigateToXRef(String xref, boolean recordCurrentPosition)
   {
      context_.panmirror().navigate(PanmirrorNavigationType.XRef, xref, recordCurrentPosition);
   }
   
   public void recordCurrentNavigationPosition()
   {
      events_.fireEvent(new SourceNavigationEvent(createSourceNavigation(getSourcePosition())));            
   }
   
   public SourcePosition getSourcePosition()
   {
      PanmirrorWidget panmirror = context_.panmirror();
      if (panmirror != null && panmirror.isAttached())
      {
         PanmirrorEditingLocation editingLocation = panmirror.getEditingLocation();
         return createSourcePosition(editingLocation.pos);
      }
      else
      { 
         return createSourcePosition(0);
      }
{'fraction_non_alphanumeric': 0.08649068322981367, 'fraction_numerical': 0.0027950310559006213, 'mean_word_length': 4.00855365474339, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '5533628', 'n_tokens_mistral': 1835, 'n_tokens_neox': 1799, 'n_words': 381}