Skip to content

Commit

Permalink
fix(vscode): header_guard works properly for edited file, no longer c…
Browse files Browse the repository at this point in the history
…auses dupe errors
  • Loading branch information
EmilyV99 authored and connorjclark committed Aug 6, 2023
1 parent 406f41d commit 888a050
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 6 deletions.
10 changes: 10 additions & 0 deletions src/base/jwinfsel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1269,6 +1269,16 @@ void derelativize_path(char* dest, char const* src_path)
strcpy(dest, result.string().c_str());
}

/* derelativize_path:
* Takes a relative path from the root directory, and returns its' absolute path.
*/
std::string derelativize_path(std::string src_path)
{
char rootpath[PATH_MAX] = {0};
get_root_path(rootpath, PATH_MAX);
return (std::filesystem::path(rootpath) / src_path).string();
}

/* jwin_file_browse_ex:
* Same as jwin_file_select but it lets you give it a list of
* possible extensions to choose from.
Expand Down
1 change: 1 addition & 0 deletions src/base/jwinfsel.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ void get_root_path(char* path, int32_t size);
void relativize_path(char* dest, char const* path);
std::string relativize_path(std::string src_path);
void derelativize_path(char* dest, char const* path);
std::string derelativize_path(std::string src_path);

typedef struct EXT_LIST
{
Expand Down
56 changes: 55 additions & 1 deletion src/parser/ScriptParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,40 @@ using std::unique_ptr;
using std::shared_ptr;
using namespace ZScript;

#include <allegro/alcompat.h>
#if (DEVICE_SEPARATOR != 0) && (DEVICE_SEPARATOR != '\0')
#define HAVE_DIR_LIST
#endif
static void get_root_path(char* path, int32_t size)
{
#ifdef HAVE_DIR_LIST
int32_t drive = _al_getdrive();
#else
int32_t drive = 0;
#endif

_al_getdcwd(drive, path, size - ucwidth(OTHER_PATH_SEPARATOR));
fix_filename_case(path);
fix_filename_slashes(path);
put_backslash(path);
}

static std::filesystem::path relativize_path(std::string src_path)
{
char rootpath[PATH_MAX] = {0};
get_root_path(rootpath, PATH_MAX);
return std::filesystem::relative(src_path, rootpath);
}

static std::filesystem::path derelativize_path(std::string src_path)
{
char rootpath[PATH_MAX] = {0};
get_root_path(rootpath, PATH_MAX);
char buf[PATH_MAX*2] = {0};
return (std::filesystem::path(rootpath) / src_path).lexically_normal();
}


extern std::vector<string> ZQincludePaths;
//#define PARSER_DEBUG

Expand Down Expand Up @@ -205,6 +239,7 @@ string* ScriptParser::checkIncludes(string& includePath, string const& importnam
return NULL;
}

extern std::vector<std::filesystem::path> force_ignores;
bool ScriptParser::valid_include(ASTImportDecl& decl, string& ret_fname)
{
if(decl.wasValidated())
Expand Down Expand Up @@ -242,10 +277,28 @@ bool ScriptParser::valid_include(ASTImportDecl& decl, string& ret_fname)
}
string filename = fname ? *fname : prepareFilename(importname); //Check root dir last, if nothing has been found yet.
ret_fname = filename;
FILE* f = fopen(filename.c_str(), "r");
//Note: If the user gives an absolute path, `relpath` will be that absolute path!
std::filesystem::path relpath = std::filesystem::path(filename).lexically_normal();
std::filesystem::path abspath = derelativize_path(filename);
FILE* f = fopen(abspath.string().c_str(), "r");
if(f)
{
fclose(f);
if(std::find(force_ignores.begin(), force_ignores.end(), abspath) != force_ignores.end())
{
decl.disable();
return true;
}
}
f = fopen(filename.c_str(), "r");
if(f)
{
fclose(f);
if(std::find(force_ignores.begin(), force_ignores.end(), relpath) != force_ignores.end())
{
decl.disable();
return true;
}
//zconsole_db("Importing filename '%s' successfully", filename.c_str());
decl.setFilename(filename);
decl.validate();
Expand All @@ -262,6 +315,7 @@ bool ScriptParser::preprocess_one(ASTImportDecl& importDecl, int32_t reclimit)
log_error(CompileError::CantOpenImport(&importDecl, filename));
return false;
}
if(importDecl.isDisabled()) return true;
unique_ptr<ASTFile> imported(parseFile(filename));
if (!imported.get())
{
Expand Down
14 changes: 12 additions & 2 deletions src/parser/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ void updateIncludePaths()
}

bool delay_asserts = false, ignore_asserts = false;
std::vector<std::filesystem::path> force_ignores;
int32_t main(int32_t argc, char **argv)
{
common_main_setup(App::zscript, argc, argv);
Expand All @@ -280,7 +281,16 @@ int32_t main(int32_t argc, char **argv)
delay_asserts = ignore_asserts = true;
else if(used_switch(argc, argv, "-delay_cassert"))
delay_asserts = true;


if(auto index = used_switch(argc, argv, "-force_ignore"))
{
for(int q = index+1; q < argc; ++q)
{
if(argv[q][0] == '-') break;
force_ignores.push_back(std::filesystem::path(argv[q]).lexically_normal());
}
}

int32_t console_path_index = used_switch(argc, argv, "-console");
if (linked && !console_path_index)
{
Expand Down Expand Up @@ -326,7 +336,7 @@ int32_t main(int32_t argc, char **argv)
argv[qr_hex_index + 1] :
// TODO: set to defaults in a better way.
"B343AFAF01C281A00DA58A4211A608DFDF080001162A0410FC5306FE2A274100381B02044031300000065824000000000000D0030000000000000000000000000000000000000000000000000000000034866C3140320000000000000000000000000000";
printf("%s\n", qr_hex.c_str());
//printf("%s\n", qr_hex.c_str());
if (qr_hex.size() != QUESTRULES_NEW_SIZE * 2)
{
zconsole_error("Error: -qr hex string must be of length %d", QUESTRULES_NEW_SIZE * 2);
Expand Down
13 changes: 12 additions & 1 deletion vscode-extension/server/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion vscode-extension/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
},
"dependencies": {
"vscode-languageserver": "^8.1.0",
"vscode-languageserver-textdocument": "^1.0.8"
"vscode-languageserver-textdocument": "^1.0.8",
"vscode-uri": "^2.1.1"
},
"scripts": {}
}
10 changes: 9 additions & 1 deletion vscode-extension/server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
TextDocumentSyncKind,
InitializeResult
} from 'vscode-languageserver/node';

import {URI} from 'vscode-uri';
import {
TextDocument
} from 'vscode-languageserver-textdocument';
Expand Down Expand Up @@ -197,10 +197,18 @@ async function processScript(textDocument: TextDocument): Promise<void> {
console.log(`Attempting to compile buffer:\n-----\n${includeText}\n-----`);
}
try {
let originPath = URI.parse(textDocument.uri).fsPath;
if(originPath.match(/[a-z]:\\.*/))
{
const letter = originPath.at(0);
if(letter) //capitalize drive letters
originPath = letter.toUpperCase()+originPath.slice(1);
}
const args = [
'-unlinked',
'-delay_cassert',
'-input', tmpInput,
'-force_ignore', originPath
];
if (settings.ignoreConstAssert)
args.push('-ignore_cassert');
Expand Down

0 comments on commit 888a050

Please sign in to comment.