<commit_before>/*
 * SessionSource.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 "SessionSource.hpp"
#include "rmarkdown/NotebookChunkDefs.hpp"

#include <string>
#include <map>

#include <boost/bind.hpp>
#include <boost/foreach.hpp>
#include <boost/utility.hpp>

#include <core/r_util/RSourceIndex.hpp>

#include <core/Log.hpp>
#include <core/Exec.hpp>
#include <core/Error.hpp>
#include <core/FilePath.hpp>
#include <core/FileInfo.hpp>
#include <core/FileSerializer.hpp>
#include <core/StringUtils.hpp>
#include <core/text/TemplateFilter.hpp>
#include <core/r_util/RProjectFile.hpp>
#include <core/r_util/RPackageInfo.hpp>

#include <core/json/JsonRpc.hpp>

#include <core/system/FileChangeEvent.hpp>

#include <r/RSexp.hpp>
#include <r/RExec.hpp>
#include <r/RInternal.hpp>
#include <r/RFunctionHook.hpp>
#include <r/RUtil.hpp>
#include <r/RRoutines.hpp>
#include <r/session/RSessionUtils.hpp>

extern "C" const char *locale2charset(const char *);

#include <session/SessionSourceDatabase.hpp>
#include <session/SessionModuleContext.hpp>
#include <session/projects/SessionProjects.hpp>

using namespace rstudio::core;

namespace rstudio {
namespace session {
namespace modules { 
namespace source {

using namespace session::source_database;

namespace {

module_context::WaitForMethodFunction s_waitForRequestDocumentSave;

void writeDocToJson(boost::shared_ptr<SourceDocument> pDoc,
                    core::json::Object* pDocJson)
{
   // write the doc
   pDoc->writeToJson(pDocJson);

   // derive the extended type property
   (*pDocJson)["extended_type"] = module_context::events()
                                   .onDetectSourceExtendedType(pDoc);

   // amend with chunk definitions if an R Markdown document
   json::Object notebook;
   if (pDoc->isRMarkdownDocument())
   {
      Error error = rmarkdown::notebook::getChunkValues(
            pDoc->path(), pDoc->id(), &notebook);
      if (error)
         LOG_ERROR(error);
   }
   (*pDocJson)["notebook"] = notebook;
   
   // discover project-specific settings when available
   r_util::RProjectConfig projConfig;
   FilePath docPath = module_context::resolveAliasedPath(pDoc->path());
   FilePath projDir = projects::projectContext().directory();
   bool hasConfig = false;
   
   if (docPath.isWithin(projDir))
   {
      projConfig = projects::projectContext().config();
      hasConfig = true;
   }
   else
   {
      Error error = r_util::findProjectConfig(docPath, &projConfig);
      hasConfig = !error;
   }
   <commit_msg>avoid inferring default project config when no project<commit_after>/*
 * SessionSource.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 "SessionSource.hpp"
#include "rmarkdown/NotebookChunkDefs.hpp"

#include <string>
#include <map>

#include <boost/bind.hpp>
#include <boost/foreach.hpp>
#include <boost/utility.hpp>

#include <core/r_util/RSourceIndex.hpp>

#include <core/Log.hpp>
#include <core/Exec.hpp>
#include <core/Error.hpp>
#include <core/FilePath.hpp>
#include <core/FileInfo.hpp>
#include <core/FileSerializer.hpp>
#include <core/StringUtils.hpp>
#include <core/text/TemplateFilter.hpp>
#include <core/r_util/RProjectFile.hpp>
#include <core/r_util/RPackageInfo.hpp>

#include <core/json/JsonRpc.hpp>

#include <core/system/FileChangeEvent.hpp>

#include <r/RSexp.hpp>
#include <r/RExec.hpp>
#include <r/RInternal.hpp>
#include <r/RFunctionHook.hpp>
#include <r/RUtil.hpp>
#include <r/RRoutines.hpp>
#include <r/session/RSessionUtils.hpp>

extern "C" const char *locale2charset(const char *);

#include <session/SessionSourceDatabase.hpp>
#include <session/SessionModuleContext.hpp>
#include <session/projects/SessionProjects.hpp>

using namespace rstudio::core;

namespace rstudio {
namespace session {
namespace modules { 
namespace source {

using namespace session::source_database;

namespace {

module_context::WaitForMethodFunction s_waitForRequestDocumentSave;

void writeDocToJson(boost::shared_ptr<SourceDocument> pDoc,
                    core::json::Object* pDocJson)
{
   // write the doc
   pDoc->writeToJson(pDocJson);

   // derive the extended type property
   (*pDocJson)["extended_type"] = module_context::events()
                                   .onDetectSourceExtendedType(pDoc);

   // amend with chunk definitions if an R Markdown document
   json::Object notebook;
   if (pDoc->isRMarkdownDocument())
   {
      Error error = rmarkdown::notebook::getChunkValues(
            pDoc->path(), pDoc->id(), &notebook);
      if (error)
         LOG_ERROR(error);
   }
   (*pDocJson)["notebook"] = notebook;
   
   // discover project-specific settings when available
   r_util::RProjectConfig projConfig;
   FilePath docPath = module_context::resolveAliasedPath(pDoc->path());
   bool hasConfig = false;
   
   if (projects::projectContext().hasProject() &&
       docPath.isWithin(projects::projectContext().directory()))
   {
      projConfig = projects::projectContext().config();
      hasConfig = true;
   }
   else
   {
      Error error = r_util::findProjectConfig(docPath, &projConfig);
      hasConfig = !error;
   }
   
{'fraction_non_alphanumeric': 0.11886854153041203, 'fraction_numerical': 0.0032701111837802484, 'mean_word_length': 4.153327716933446, 'pattern_counts': {'":': 0, '<': 61, '<?xml version=': 0, '>': 71, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1999343', 'n_tokens_mistral': 1937, 'n_tokens_neox': 1871, 'n_words': 467}