<commit_before>/*
 * SessionWorkbench.cpp
 *
 * Copyright (C) 2009-12 by RStudio, Inc.
 *
 * 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.
 *
 */


#include "SessionWorkbench.hpp"

#include <algorithm>

#include <boost/function.hpp>
#include <boost/format.hpp>

#include <core/Error.hpp>
#include <core/Debug.hpp>
#include <core/Exec.hpp>
#include <core/StringUtils.hpp>
#include <core/FileSerializer.hpp>

#include <core/http/Request.hpp>
#include <core/http/Response.hpp>

#include <core/json/JsonRpc.hpp>

#include <core/system/Environment.hpp>
#include <core/system/ShellUtils.hpp>

#include <r/ROptions.hpp>
#include <r/session/RSession.hpp>
#include <r/session/RClientState.hpp> 
#include <r/RFunctionHook.hpp>
#include <r/RRoutines.hpp>

#include <session/projects/SessionProjects.hpp>

#include <session/SessionModuleContext.hpp>
#include <session/SessionUserSettings.hpp>
#include <session/SessionConsoleProcess.hpp>
#include <session/RVersionSettings.hpp>

#include "SessionVCS.hpp"
#include "SessionGit.hpp"
#include "SessionSVN.hpp"

#include "SessionSpelling.hpp"

#include <R_ext/RStartup.h>
extern "C" SA_TYPE SaveAction;

#include "session-config.h"
#ifdef RSTUDIO_SERVER
#include <core/system/Crypto.hpp>
#endif


using namespace rstudio::core;

namespace rstudio {
namespace session {
namespace modules { 
namespace workbench {

namespace {

module_context::WaitForMethodFunction s_waitForActiveDocumentContext;

SEXP rs_getActiveDocumentContext()
{
   // prepare context event
   ClientEvent activeDocumentContextEvent(client_events::kGetActiveDocumentContext);
   
   // wait for event to complete
   json::JsonRpcRequest request;
   
   bool succeeded = s_waitForActiveDocumentContext(&request, activeDocumentContextEvent);
   if (!succeeded)
      return R_NilValue;
   
   std::string id;
   std::string path;
   std::string contents;
   std::string selection;
   json::Array rangeJson;
   
   Error error = json::readObjectParam(request.params, 0,
                                       "id", &id,
                                       "path", &path,
                                       "contents", &contents,
                                       "selection", &selection,
                                       "range", &rangeJson);
   if (error)
   {
      LOG_ERROR(error);
      return R_NilValue;
   }
   
   std::vector<int> range;
   if (!json::fillVectorInt(rangeJson, &range))
   {
      LOG_WARNING_MESSAGE("failed to parse document range");
   }
   
   using namespace r::sexp;
   Protect protect;
   ListBuilder builder(&protect);<commit_msg>transform ranges from 0-based to 1-based indexing<commit_after>/*
 * SessionWorkbench.cpp
 *
 * Copyright (C) 2009-12 by RStudio, Inc.
 *
 * 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.
 *
 */


#include "SessionWorkbench.hpp"

#include <algorithm>

#include <boost/function.hpp>
#include <boost/format.hpp>

#include <core/Error.hpp>
#include <core/Debug.hpp>
#include <core/Exec.hpp>
#include <core/StringUtils.hpp>
#include <core/FileSerializer.hpp>

#include <core/http/Request.hpp>
#include <core/http/Response.hpp>

#include <core/json/JsonRpc.hpp>

#include <core/system/Environment.hpp>
#include <core/system/ShellUtils.hpp>

#include <r/ROptions.hpp>
#include <r/session/RSession.hpp>
#include <r/session/RClientState.hpp> 
#include <r/RFunctionHook.hpp>
#include <r/RRoutines.hpp>

#include <session/projects/SessionProjects.hpp>

#include <session/SessionModuleContext.hpp>
#include <session/SessionUserSettings.hpp>
#include <session/SessionConsoleProcess.hpp>
#include <session/RVersionSettings.hpp>

#include "SessionVCS.hpp"
#include "SessionGit.hpp"
#include "SessionSVN.hpp"

#include "SessionSpelling.hpp"

#include <R_ext/RStartup.h>
extern "C" SA_TYPE SaveAction;

#include "session-config.h"
#ifdef RSTUDIO_SERVER
#include <core/system/Crypto.hpp>
#endif


using namespace rstudio::core;

namespace rstudio {
namespace session {
namespace modules { 
namespace workbench {

namespace {

module_context::WaitForMethodFunction s_waitForActiveDocumentContext;

SEXP rs_getActiveDocumentContext()
{
   // prepare context event
   ClientEvent activeDocumentContextEvent(client_events::kGetActiveDocumentContext);
   
   // wait for event to complete
   json::JsonRpcRequest request;
   
   bool succeeded = s_waitForActiveDocumentContext(&request, activeDocumentContextEvent);
   if (!succeeded)
      return R_NilValue;
   
   std::string id;
   std::string path;
   std::string contents;
   std::string selection;
   json::Array rangeJson;
   
   Error error = json::readObjectParam(request.params, 0,
                                       "id", &id,
                                       "path", &path,
                                       "contents", &contents,
                                       "selection", &selection,
                                       "range", &rangeJson);
   if (error)
   {
      LOG_ERROR(error);
      return R_NilValue;
   }
   
   std::vector<int> range;
   if (!json::fillVectorInt(rangeJson, &range))
   {
      LOG_WARNING_MESSAGE("failed to parse document range");
   }
   
   // the 'range' above is passed as a 0-indexed object;
   // transform to 1-based indexing for the R side
   for (std::size_t i = 0; i < range.size(); ++i)
      ++range[i];
   
   using namespace r::sexp;
   Protect protect;
   ListBuilder builder(&protect);
{'fraction_non_alphanumeric': 0.10855054036872219, 'fraction_numerical': 0.003973299427844882, 'mean_word_length': 3.3762169680111267, 'pattern_counts': {'":': 0, '<': 56, '<?xml version=': 0, '>': 55, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2548993', 'n_tokens_mistral': 1934, 'n_tokens_neox': 1870, 'n_words': 488}