Debian Codegraph @

LanguageServer/PathConverterDefault.cpp

raw
1 #include "PathConverterDefault.hpp"
2 #include "SFTPClientData.hpp"
3 #include "file_logger.h"
4 #include "fileutils.h"
5 #include "globals.h"
6 #include "ieditor.h"
7 #include <imanager.h>
8 #include <wx/filesys.h>
9 #include <wx/regex.h>
10
11 LSP::FilePath PathConverterDefault::ConvertFrom(const wxString& path) const
12 {
13 // convert network path to local path
14 wxString decodedPath = FileUtils::DecodeURI(path);
15 if(decodedPath.StartsWith("file://")) {
16 decodedPath.Remove(0, wxStrlen("file://"));
17 }
18
19 #ifdef __WXMSW__
20 // On Windows, path is now set to
21 // /C:/src/path/to/file/a.cpp, the following code removes the leading slash
22 if(decodedPath.length() >= 3 && decodedPath[0] == '/' && decodedPath[2] == ':') {
23 decodedPath.Remove(0, 1); // remove the leading slash
24 }
25 #endif
26
27 LSP::FilePath fp(decodedPath);
28
29 #ifdef __WXMSW__
30 if(decodedPath.StartsWith("/")) {
31 // UNIX path on Windows !?
32 // Check for open SFTP files
33 IEditor::List_t editors;
34 clGetManager()->GetAllEditors(editors);
35 for(auto editor : editors) {
36 if(editor->GetClientData("sftp")) {
37 SFTPClientData* pcd = dynamic_cast<SFTPClientData*>(editor->GetClientData("sftp"));
38 if(pcd) {
39 const wxString& remotePath = pcd->GetRemotePath();
40 if(decodedPath == remotePath) {
41 // it's a remote file
42 return pcd->GetLocalPath();
43 }
44 }
45 }
46 }
47 fp.SetIsRemoteFile(true);
48 }
49 #else
50 // if the path does not exist on our file system, assume a remote file
51 if(!wxFileName::FileExists(decodedPath)) {
52 fp.SetIsRemoteFile(true);
53 }
54 #endif
55 // default path conversion
56 return fp;
57 }
58
59 LSP::FilePath PathConverterDefault::ConvertTo(const wxString& path) const
60 {
61 clDEBUG() << "Converting" << path;
62 wxFileName fn(path);
63
64 // Locate an editor with this file path and check to see if it is a remote file
65 IEditor* editor = clGetManager()->FindEditor(fn.GetFullPath());
66 if(editor && editor->IsRemoteFile()) {
67 wxString url = editor->GetRemotePath();
68 url.Prepend("file://");
69 clDEBUG() << path << "->" << url;
70 return url;
71 } else {
72 // default behavior
73 wxString url = wxFileSystem::FileNameToURL(fn);
74 clDEBUG() << path << "->" << url;
75 return LSP::FilePath(url);
76 }
77 }