texstudio-org / texstudio

TeXstudio is a fully featured LaTeX editor. Our goal is to make writing LaTeX documents as easy and comfortable as possible.
http://www.texstudio.org/
GNU General Public License v3.0
2.85k stars 346 forks source link

Insert filename on the files sidebar inserts folder name instead #3808

Closed nathanaeljsmith2 closed 2 months ago

nathanaeljsmith2 commented 2 months ago

Environment

Expected behavior

Right clicking on a file in the Files tab on the sidebar, and choosing Insert filename, should insert the filename into the document.

Actual behavior

Right clicking on a file in the Files tab on the sidebar, and choosing Insert filename, inserts the folder name into the document.

octaeder commented 2 months ago

same for me, win10, current build

foreachthing commented 2 months ago

I agree with @nathanaeljsmith2. Same thing with my setup:

octaeder commented 2 months ago

texstudio.cpp, l. 5355: add File to absolutPath: const QString fn=getRelativeFileName(fi.absoluteFilePath(),rootDir); This gives the file name including the relative directory path beginning from the tex file folder shown in the side pane: abc/myfile.jpg. I'm not sure, maybe abc should not be present.

foreachthing commented 2 months ago

I'm not sure, maybe abc should not be present.

I think it should. If I want to include a file, the subfolder needs to be there. Same for images.

octaeder commented 2 months ago

it is a little bit complicated to explain: abc/ is the folder of the tex file to compile, i.e. abc/ is the folder whose files and folders are shown in the side pane. If there is a folder abc/pics then pics/ should be in the path. I assume that tex won't find the files when adding abc/.

octaeder commented 2 months ago

The problem is in utilsSystem.cpp (l. 497ff):

QString getRelativeBaseNameToPath(const QString &file, QString basepath, bool baseFile, bool keepSuffix)
{
    basepath.replace(QDir::separator(), "/");
    if (basepath.endsWith("/")) basepath = basepath.left(basepath.length() - 1);

    QFileInfo fi(file);
    QString filename = fi.fileName();
    QString path = fi.path();
    if (path.endsWith("/")) path = path.left(path.length() - 1);
    QStringList basedirs = basepath.split("/");
    if (baseFile && !basedirs.isEmpty()) basedirs.removeLast();

Var basdir is the second parameter with value rootDir set by caller. The value is ok, but last line shown here removes the last/lowest dir from the path (the dir containing the tex file). After that anything in commen is removed from basdirs and dirs. The difference remaining is the dir removed previously and will be added to the output.

I think you can't use getRelativeBaseNameToPath in this case.

sunderme commented 2 months ago

fixed with 04bdbca9ba773dfd1786d4ef2eaf70f989c48a1b

octaeder commented 2 months ago

@sunderme your solution inserts the filename (i.e. including suffix). Usually the suffix is missing. Folders are inserted without slash at the end so it is not clear from the text if this is a file or not.

sunderme commented 2 months ago

I don't understand. It is a user triggered action, supposedly to put filename/foldername easily into a latex function and that is what is achieved ?

octaeder commented 2 months ago

The folder is a special case that my solution implemented, but not necessary (pics/). Inserting files: until now used: \include{subdoc} and \includegraphics{image}. For the latter you can define which extension should be used. Those who want to do so need to remove the suffix. But ok, maybe a corner case.